Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq syntax to select nested objects

Tags:

.net

linq

I'm trying to filter an IEnumerable using some of its nested properties.

I've racked my brain every which way but cannot find a solution to this query. In fact I don't event know whether this is possible.

Its structure is like this

T is a Deal a Deal has a collection of Subcategories (the collection may be empty) a Subcategory has a SubcategoryId

I want to be able to select Deals whether they have a SubcategoryId = parameter that is passed to method.

like image 763
dotnetnoob Avatar asked Jun 01 '26 04:06

dotnetnoob


1 Answers

public IEnumerable<Deal> FindDeals(IEnumerable<Deal> source, int subCategoryId)
{    
    return source.Where(d => d.Subcategories.Any(s => s.Id == subCategoryId));
}
like image 148
Lee Avatar answered Jun 03 '26 17:06

Lee