c# - 包括一个集合,然后是下一级的集合

标签 c# linq entity-framework

我正在尝试使用 EF 7 查询集合和子集合。代码如下:

public async Task < List < Table >> GetAllTable() {
  var tableList = await db.Tables.Include(o => o.Checks.Select(i => i.CheckItems)).ToListAsync();
  return tableList;

}

我遵循这里的语法 MSDN .但是,当我运行此代码时,出现以下错误。有谁知道这里出了什么问题?谢谢!

InvalidCastException: Unable to cast object of type 'Remotion.Linq.Clauses.Expressions.SubQueryExpression' to type 'System.Linq.Expressions.MemberExpression'.** Microsoft.Data.Entity.Query.EntityQueryModelVisitor.b__30_2(<>f__AnonymousType2`2 <>h__TransparentIdentifier0)

最佳答案

您正在阅读的文档适用于 EF 5。

design meeting notes对于 EF 7,说它的语法已经改变 - 试试这个:

db.Tables.Include(t => t.Checks)
    .ThenInclude(c => c.CheckItems)
    .ToListAsync()

关于c# - 包括一个集合,然后是下一级的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30151490/

相关文章:

c# - 从数组\列表中的字符串中删除非常量重复的单词

c# - Entity Framework 6 自动编译查询

c# - 值不能为空 : connection while testing database

c# - 以编程方式运行项目

c# - linq中的子串

c# - 使用 Linq Any() 和 All() 的 TDD

c# - 多重性在关系 : EF code first one to one relationship with same primary key and foreign key 中的角色中无效

c# - 在 Entity Framework 中关闭事务

c# - 在每次单击按钮时添加行数据 GridView

c# - 如何更改 telerik WPF RadGridView 列过滤 View 模板