开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜