Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - find element in nested collections

Tags:

c#

.net

vb.net

linq

I have a generic list - SupportedTypeGroups.

Each SupportedTypeGroup has SupportedTypes property (generic list of SupportedType).

How to construct a Linq query to locate SupportedType with required name?

like image 237
SharpAffair Avatar asked Feb 19 '11 16:02

SharpAffair


2 Answers

var result = SupportedTypeGroups
             .SelectMany(g => g.SupportedTypes)
             .FirstOrDefault(t => t.Name == "TypeName");
like image 181
Snowbear Avatar answered Sep 27 '22 22:09

Snowbear


SupportedTypeGroups
  .SelectMany(s => s.SupportedTypes)
  .Where(s => s.name == "TheName");
like image 36
Magnus Avatar answered Sep 27 '22 21:09

Magnus