不能包含具有实体查询的嵌套列表的对象

时间:2017-05-30 11:04:34

标签: linq entity-framework-core

我正在使用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 =&gt; {来自Bar in o   选择[x] .Xpto}'无效。表达式应代表a   物业访问:'t =&gt; t.MyProperty”。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

仅针对使用https://www.youtube.com/watch?v

面临同样问题的其他人
 context.Foo.Include(o => o.Bars).ThenInclude(x => x.Xpto).ToList();

你可能没有获得任何Intellisense帮助,但它仍然适用于ef核心。