开发者

Correct way to write a LINQ or Lambda to retrieve items with a matching supplier ID

开发者 https://www.devze.com 2023-04-02 00:39 出处:网络
Given the following classes Product - int ProductID - Supplier[] Suppliers ... Supplier - int SupplierID - string SupplierName

Given the following classes

Product

- int ProductID

- Supplier[] Suppliers

...

Supplier

- int SupplierID

- string SupplierName

...

If I have an array of 1000 products, an开发者_如何转开发d each product can contain multiple suppliers, how would I get supplier ID [X] from the array of Products?


var suppliers = 
    from product in products
    from supplier in product.Suppliers
    where supplier.SupplierID == X
    select supplier;

Or use the extension method equivalent:

var suppliers = 
    products.SelectMany(p => p.Suppliers).Where(s => s.SupplierID == X);
0

精彩评论

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