【问题标题】:linq query which will return single values and a list in one calllinq 查询将在一次调用中返回单个值和一个列表
【发布时间】:2016-08-17 08:15:52
【问题描述】:

我有这个 linq 调用:

PropertyViewModel propertyModel = null;
    propertyModel = (from property in db.LoanProperties
                    join tenant in db.Tenants
                    on property.PropertyId equals tenant.PropertyId
                    where property.LoanApplicationId == newApplication.LoanId
                    select new PropertyViewModel(
                        propertyModel.AddressLine1 = property.AddressLine1,
                        propertyModel.AddressLine2 = property.AddressLine2,
                        propertyModel.TotalRentPerAnnum = property.TotalRentPerAnnum,
                        propertyModel.Tenants = db.Tenants.Where(s => s.PropertyId == property.PropertyId).ToList()
                        ));

我的视图模型:

public class PropertyViewModel
    {
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public decimal? TotalRentPerAnnum { get; set; }
        public List<TenantViewModel> Tenants { get; set; }
    }

我想从我的 TenantViewModel 中的 linq 查询中转换属性下的租户。

我怎样才能做到这一点?

我指的是propertyModel.Tenants的最后一行。

【问题讨论】:

  • 然后在其末尾添加一个 .select 并进行投射
  • 那么实际的问题是什么?
  • db.Tenants.Where(...).Select(t =&gt; new TenantViewModel { ... }
  • 为什么你要加入物业和租户,然后再分别查询db.Tennats db.Tenants.Where(s =&gt; s.PropertyId == property.PropertyId).ToList()
  • @AdliMammadov 第一次加入可以去

标签: c# linq linq-to-sql linq-to-entities linq-to-objects


【解决方案1】:

这是我的第一个答案!希望对您有所帮助:

Tenants 和 LoanProperties 对象必须具有带有外键 PropertyId 的 navigation property。因此,LoanProperties 对象必须有一个租户列表。

我更喜欢您在最后一行使用的lambda expressions(干净/清晰的代码)。 试试这个:

var propertyModel = db.LoanProperties
        .Where(p => p.LoanApplicationId == newApplication.LoanId)
        .Select(p => new PropertyViewModel(){
                AddressLine1 = p.AddressLine1,
                AddressLine2 = p.AddressLine2,
                TotalRentPerAnnum = p.TotalRentPerAnnum,
                Tenants = p.Tenants.Select(t=> new TenantViewModel(){TenantType = t.TenantType , //other properties...})
                })
                //you don't have to query again, the tenants are already in the LoanProperty objects
                //you just have to transform it on ViewModel with a Select
        .FirstOrDefault();

另外,在你的PropertyViewModel的构造方法中,你不必把propertyModel.--- = ----.写进去也没用。

【讨论】:

  • @Laziale,正如 woodykiddy 所说,您应该使用 new TenantViewModel(){TenantType = p.tenantType}。我编辑
【解决方案2】:

希望我正确理解了您的问题。我猜您正在寻找将您的租户数据库对象与您的 TenantViewModel 映射?

ropertyViewModel propertyModel = null;
    propertyModel = (from property in db.LoanProperties
                    join tenant in db.Tenants
                    on property.PropertyId equals tenant.PropertyId
                    where property.LoanApplicationId == newApplication.LoanId
                    select new PropertyViewModel(
                        propertyModel.AddressLine1 = property.AddressLine1,
                        propertyModel.AddressLine2 = property.AddressLine2,
                        propertyModel.TotalRentPerAnnum = property.TotalRentPerAnnum,
                        propertyModel.Tenants = db.Tenants.Where(s => s.PropertyId == property.PropertyId).Select(t => new TenantViewModel {//map your properties}).ToList()
                        ));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多