编写LINQ或Lambda以检索具有匹配供应商ID的项目的正确方法

Sam*_*Sam 1 c# linq lambda

鉴于以下课程

产品
- int ProductID
- 供应商[]供应商
......

Supplier
- int SupplierID
- string SupplierName
...

如果我有1000个产品的阵列,并且每个产品可以包含多个供应商,我如何从产品阵列中获得供应商ID [X]?

Ste*_*ven 5

var suppliers = 
    from product in products
    from supplier in product.Suppliers
    where supplier.SupplierID == X
    select supplier;
Run Code Online (Sandbox Code Playgroud)

或者使用等效的扩展方法:

var suppliers = 
    products.SelectMany(p => p.Suppliers).Where(s => s.SupplierID == X);
Run Code Online (Sandbox Code Playgroud)