简体   繁体   English

Linq多级内部联接

[英]Linq multi level inner join

I have 3 diffferent entities: 我有3个不同的实体:

E1, E2, E3

E1 has a property that is an ObservableCollection of E2. E1的属性是E2的ObservableCollection。 E2 has a property that is an ObservableCollection of E3. E2的属性是E3的ObservableCollection。

And with a linq query, I want to get all E3 based on E1 I got as starting point. 并通过linq查询,我想基于我作为起点的E1获得所有E3。

I tried something like that : 我尝试过这样的事情:

Query = Query.Where<E3>(i => E1.E2List.Select(ep => ep.E3List.Select(ai => ai.id))
             .Contains(i.id));

Basically meaning : getting all E3 where it's id is in the list of E3 in the lists of E2 from E1 基本上的意思是:从E1获取E2列表中E3列表中所有ID为ID的E3

But as you guess, this doesn't work. 但是正如您所猜测的那样,这是行不通的。

My starting point is this :
//make the array 
E3[] array;
//get all E3 datas
IQueryable<E3> Query = DBSetE3.AsQueryable();

And I must have all the results in my array 我必须将所有结果都放在数组中

So my result should be : getting in array, all E3 objects that are linked into E2 that are contains into E1, based on E1. 所以我的结果应该是:基于E1,进入数组,链接到E2的所有E3对象都包含在E1中。

For example : 例如 :

Entity 1| List of E2
E1 | E2A, E2B

Entity 2 | List of E3
E2B | E3C, E3D
E2A | E3E, E3F

When I search for E3 linked to E1, I must get E3C, E3D,E3E,E3F 当我搜索链接到E1的E3时,我必须获得E3C,E3D,E3E,E3F

You can do something like this: 您可以执行以下操作:

var e1Ids = new [] { 1, 2, 3 };
var e3s = DBSetE1.AsQueryable().Where(e => e1Ids.Contains(e.Id))
                               .SelectMany(e => e.E2List)
                               .SelectMany(e => e.E3List);

Basically, you filter all the E1's based on your input. 基本上,您可以根据输入过滤所有E1。 Then you want to SelectMany (which flattens the the result [[1,2],[3,4]] into [1,2,3,4] ) to grab the E2s. 然后,您想要SelectMany (将结果[[1,2],[3,4]][1,2,3,4] )来抓住E2。 Then you SelectMany it's E3s. 然后,你SelectMany它的E3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM