Linq Select the submenu of the main list.

I have a main list of complex objects.

I have a list of identifiers that I need to select from a complex object from the main list.

this does not work

MasterListofComplexObj.Where(u => MasterListofComplexObj.Select(i => i.Id).Contains(ChildListofIntIds)); 

Any help would be appreciated.

+6
source share
3 answers

This should work:

 var results = MasterListofComplexObj.Where(u => ChildListofIntIds.Contains(u.Id)); 
+20
source
 var results = from obj in MasterListofComplexObj where ChildListofIntIds.Contains(obj.Id) select obj; 

It is IEnumerable. You may want .FirstOrDefault () to get one object.

Translates MasterListofComplexObj.Where (item => ChildListofIntIds.Contains (item.Id))

No selection is required if you want the object itself, and not one of its properties.

+1
source

Another, more general thing to look out for is Join:

 var results = MasterList.Join(ChildList, (m => m.Id), (c => c), ((m,c) => m)); 

I believe, but cannot support quotes or experimental data, that Join will be faster than Where β†’ Contains.

+1
source

Source: https://habr.com/ru/post/907256/


All Articles