开发者

Find an element in a collection of collection

开发者 https://www.devze.com 2023-03-14 08:04 出处:网络
How to use Linq to find an ite开发者_StackOverflow中文版m in a List of ProductCat where every ProductCat contains a List of Product.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消