Linq - find an item in nested collections

I have a general list - SupportedTypeGroups.

Each supported TypeGroup has the SupportedTypes property (general list of SupportedType).

How to build a Linq query to search for SupportedType with the required name?

+6
c # linq
source share
3 answers
var result = SupportedTypeGroups .SelectMany(g => g.SupportedTypes) .FirstOrDefault(t => t.Name == "TypeName"); 
+8
source share
 SupportedTypeGroups .SelectMany(s => s.SupportedTypes) .Where(s => s.name == "TheName"); 
+5
source share

Assuming SupportedTypes is IEnumerable<SupportedType>

 from g in SupportedTypeGroups where g.SupportedTypes.Where(t => t.Name == "magicName") select g; 

Assuming SupportedTypes is just a SupportedType property

 from g in SupportedTypeGroups where g.SupportedTypes.Name == "magicName" select g; 

If you just want SupportedType

 from tg in SupportedTypeGroups from t in tg.SupportedTypes where t.Name == "magicName" select t; 
+1
source share

All Articles