2017-05-30 42 views
0

嵌套列表的對象我使用實體框架的核心,我有以下結構:不能包括與實體查詢

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 => {從酒吧在鄰 選擇[x] .Xpto}' 是無效的。該表達式應代表 屬性訪問:'t => t.MyProperty'。

我在這裏錯過了什麼?

+1

https://stackoverflow.com/a/36601380/5621827這可能有幫助 – jitender

+0

它does not。在我的'ThenInclude'中,我只能訪問'Collection'屬性。我嘗試使用'Select'來獲得我想要的字段,但它引發錯誤 –

+1

當您編寫ThenInclude(o => o.Xpto)時收到的錯誤是什麼?因爲如果您看到上述文章的評論,您將無法獲得任何Intellisense的幫助,但它仍然有效 – jitender

回答

2

只是別人利用面臨this

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

你可能不會得到任何智能感知幫助,但它仍然會與EF核心工作了同樣的問題。