我正在尝试在两个具有导航属性的实体上进行左连接。我已经禁用了延迟加载。
这是我的代码:
var awis =
from ai in Context.AdItems
.Include(ai => ai.Item)
.Include(ai => ai.Item.Buyer)
.Include(ai => ai.Item.OrderHeader)
.Where(ai => ai.AdYear == adYear && ai.AdNumber == adNumber)
join si in Context.StoreItems
.Include(si => si.Store)
.Where(si => si.StoreId == storeId) on ai.ItemId equals si.ItemId into x
from r in x.DefaultIfEmpty()
select new AdWeekItem
{
AdItemId = ai.AdItemId,
AdItem = ai,
StoreItemId = r == null ? 0 : r.StoreItemId,
StoreItem = r
};
外部联接工作正常但我没有在预计AdWeekItem
中包含我的导航属性。
即AdWeekItem.AdItem.Item
为空,等等。
如何包含这些导航属性并执行左外连接?
答案 0 :(得分:2)
在EF6中,当LINQ查询以投影(Include
)结束时,select new ...
为always ignored。您唯一的选择是显式查询您想要包含的导航属性,然后投影到最终结果:
var temp = from ai in Context.AdItems
.Where(ai => ai.AdYear == adYear && ai.AdNumber == adNumber)
join si in Context.StoreItems
.Where(si => si.StoreId == storeId) on ai.ItemId equals si.ItemId into x
from r in x.DefaultIfEmpty()
select new
{
AdItemId = ai.AdItemId,
AdItem = ai,
ai.Item,
ai.Item.Buyer,
ai.Item.OrderHeader,
StoreItemId = r == null ? 0 : r.StoreItemId,
StoreItem = r,
r.Store
};
var awis = from x in temp.AsEnumerable() // pull into memory and continue
select new AdWeekItem
{
AdItemId = x.AdItemId,
AdItem = x.AdItem,
StoreItemId = x.StoreItemId,
StoreItem = x.StoreItem
};
EF将通过 relationship fixup (即导航属性的自动填充)填充AdItem.Item
等。