如果我使用联接,Include()方法就不再起作用,例如:
from e in dc.Entities.Include("Properties")
join i in dc.Items on e.ID equals i.Member.ID
where (i.Collection.ID == collectionID)
select e未加载e.Properties
如果没有连接,Include()将起作用
李
发布于 2009-05-06 00:52:50
更新:实际上,我最近添加了另一个技巧,涵盖了这一点,并提供了一个替代的可能更好的解决方案。其想法是将Include()的使用延迟到查询结束,有关更多信息,请参阅以下内容:Tip 22 - How to make include really include
在使用Include()时,实体框架中存在已知的限制。Include不支持某些操作。
看起来你可能在这些限制上遇到了一个问题,要解决这个问题,你应该尝试这样做:
var results =
from e in dc.Entities //Notice no include
join i in dc.Items on e.ID equals i.Member.ID
where (i.Collection.ID == collectionID)
select new {Entity = e, Properties = e.Properties};这将返回属性,如果实体和属性之间的关系是一对多(但不是多对多),您将发现每个生成的匿名类型在中具有相同的值:
anonType.Entity.Properties
anonType.Properties这是实体框架中一个称为关系修复的功能的副作用。
有关详细信息,请参阅my EF Tips series中的此Tip 1。
发布于 2010-04-20 03:19:00
试试这个:
var query = (ObjectQuery<Entities>)(from e in dc.Entities
join i in dc.Items on e.ID equals i.Member.ID
where (i.Collection.ID == collectionID)
select e)
return query.Include("Properties") 发布于 2009-04-28 13:31:01
那么"Entity“上与"Item.Member”相关的导航属性的名称是什么(即导航的另一端)。您应该使用这个,而不是连接。例如,如果"entity“添加一个基数为1的名为Member的属性,而Member有一个基数为do的属性Items,您可以这样做:
from e in dc.Entities.Include("Properties")
where e.Member.Items.Any(i => i.Collection.ID == collectionID)
select e我在这里猜测您的模型的属性,但这应该会让您大致了解。在大多数情况下,在LINQ to Entities中使用join是错误的,因为它表明您的导航属性设置不正确,或者您没有使用它们。
https://stackoverflow.com/questions/794283
复制相似问题