简体   繁体   English

在逆属性上使用EF 4.1时如何使用linq的.include()?

[英]How to use linq's .Include() when using EF 4.1 on an inverse property?

I am having trouble figuring out how to use .Include() to be able to access this nested relationship. 我在弄清楚如何使用.Include()才能访问此嵌套关系时遇到了麻烦。 I tried what seemed like the most direct way but intellisense doesn't populate and when I hand enter the relation there is a compilation error. 我尝试了看似最直接的方法,但是智能感知并没有填充,当我手动输入关系时,出现了编译错误。

I have this db mapped setup 我有这个数据库映射设置

public class W
{
 public int WId { get; set; }
 public virtual ICollection<T> Ts { get; set; }
}

public class T
{
 public int TId { get; set; }
 public int WId { get; set; }
 public int CId { get; set; }
 public virtual W W { get; set; }
 public virtual C { get; set; }
}

public class C
{
 public int CId { get; set; }
 public virtual ICollection<T> Ts { get; set; }
}

I have a get method which queries 我有一个查询的方法

//set in constructor
DbSet<W> dbSet;
DbContext context;

//called in method
IQueryable<W> query = dbSet;
query = query.Include(w => w.Ts);//works
query = query.Include(w => w.Ts.C); //I wanted to do this but it doesn't work

Can I use .Include() like this? 我可以这样使用.Include()吗?

It seemed like it was going to work with no hitch but apparently I am doing it wrong. 似乎可以顺利进行,但显然我做错了。 I would prefer not to have to iterate through the Ts collection and then populate their C manually each time. 我希望不必遍历Ts集合,然后每次手动填充C。

您可以使用以下语法:

query = query.Include(w => w.Ts.Select(t => t.C));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM