实体框架到具有可为null的外键的组查询中



我想做一个"进入组查询";例如,在可以为null的外键上,基于customer.LocationId获取客户的所有信用。但是如果customer.LocationId为null,我希望列表为null或为空。

但是使用下面的代码,EF会忽略LocationId为空的客户

var customers = from c in db.Customers
join lc in db.LocationCredits on c.LocationId equals lc.Id into credits
select new 
{
CustomerName = c.Name,
Credits = credits
};

有什么想法吗?

您可以尝试此查询

var customers  = db.Customers
.include(m=> m.Locations)
.where(m=> m.LocationId != null)
.select (m=> new {
Customername = m.Name,
Credits = Credits
})

最新更新