I have a class that contains an inner list of the same class, For example:
class Foo
{
string SearchId;
List<Foo> GroupedPackages
}
I want to return the first "foo" instance that fits a condition, it can be in the main instance or in the inner List.
This is what I have so far - it is a bit ugly but it works:
Package = response.lst.Where(p => p.SearchId == SearchId ||
(p.GroupedPackages != null &&
p.GroupedPackages.Any(m => m.SearchId==SearchId)))
.FirstOrDefault();
if (Package != null)
{
if (Package.SearchId != SearchId)
{
Package = Package.GroupedPackages.FirstOrDefault(m => m.SearchId == SearchId);
}
}
Where "response.lst" is a List of foo and Package is foo.
I want if possible to do it in a one line lambda expression
This selects the first
Foo
with the specifiedSearchId
both on top level or in any of theFoo
instances inGroupedPackages
.