How to use Linq to find an ite开发者_StackOverflow中文版m in a List of ProductCat where every ProductCat contains a List of Product.
I want to find the Product by ProductID.
Thanks
Product foundProduct = productCatList .SelectMany(list => list.Products) .Where(product => product.ProductID == whateverID) .SingleOrDefault()
Try this:
var results = from pc in myProductCats
where pc.Products.Contains(ProductId)
select pc;
You question is missing a lot of details. If you provide some code and the class structure I can provide a better answer.
For a pure Linq language syntax:
var pds = from pc in prodCats
select from p in pc.Products where p.ProductID < 3
select p;
But I think the IEnumerable extension + lambda version is more readable.
精彩评论