我无法弄清楚如何使用.Include()
来访问此嵌套关系。我尝试了看起来最直接的方法,但是intellisense没有填充,当我输入关系时会出现编译错误。
我有这个数据库映射设置
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; }
}
我有一个查询
的get方法//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
.Include()
吗?似乎它没有任何障碍,但显然我做错了。我宁愿不必遍历Ts集合,然后每次手动填充它们。
答案 0 :(得分:4)
您可以使用以下语法:
query = query.Include(w => w.Ts.Select(t => t.C));