LINQ从子列表中选择

如何使Linq查询从类别中获取所有Productpricediscounts?

public class ProductCategory { public List categoryProducts; } public class Product { public List productPrices; } public class Productprice { public List priceDiscounts; } 

我的查询必须看起来像:

 categoryProducts.Select(p => p.productPrices).Select(x => x.?!?! 

问题是我本来期望x。 – intellisense建议priceDiscounts ,但我得到“list” – 值(如:“Any”,“Select”,“Distinct”等等。)

您需要使用SelectMany才能访问priceDiscounts

 var query = categoryProducts .SelectMany(x => x.productPrices) .SelectMany(y => y.priceDiscounts); 

你需要Enumerable.SelectMany

 var result = categoryProducts.SelectMany(x => x.productPrices) .SelectMany(x => x.priceDiscounts);