【发布时间】: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