【问题标题】:Can't include objects of nested list with Entity query不能在实体查询中包含嵌套列表的对象
【发布时间】:2017-05-30 19:36:30
【问题描述】:

我正在使用 Entity Framework Core,我有以下结构:

class Foo
{
    public ICollection<Bar> Bars { get; set; }
}

class Bar
{
    public XPTO Xpto { get; set; }
}

class XPTO
{
    public string Message { get; set; }
}

如何在我的 Linq 查询中包含 XPTO 对象?

我试过了:

context.Foo.Include(o => o.Bars).ToList(); // This gets me the Bars but the XPTO's of them are null

context.Foo.Include(o => o.Bars).ThenInclude(o => o.Select(x => x.Xpto)).ToList(); This throws an error:

System.ArgumentException: 属性表达式 'o => {from Bar in o 选择 [x].Xpto}' 无效。表达式应该代表一个 属性访问:'t => t.MyProperty'。

我在这里错过了什么?

【问题讨论】:

  • stackoverflow.com/a/36601380/5621827 这可能会有所帮助
  • 它没有。在我的ThenInclude 中,我只能访问Collection 属性。我尝试使用 Select 来获取我想要的字段,但它抛出错误
  • 编写 ThenInclude(o => o.Xpto) 时收到的错误是什么?因为如果您看到上面帖子的评论,您将不会获得任何 Intellisense 帮助,但它仍然有效
  • facepalm 是的,这行得通。我想因为我的收藏品会有所不同。对不起。谢谢您的帮助! :D
  • 不高兴它可以帮助你

标签: linq entity-framework-core


【解决方案1】:

仅针对使用 this 的其他人面临相同问题

 context.Foo.Include(o => o.Bars).ThenInclude(x => x.Xpto).ToList();

您可能无法获得任何 Intellisense 帮助,但它仍然适用于 ef 内核。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多