[英]LINQ Select from sub-list
如何使Linq查詢從類別中獲取所有Productpricediscounts?
public class ProductCategory
{
public List<Product> categoryProducts;
}
public class Product
{
public List<Productprice> productPrices;
}
public class Productprice
{
public List<Productpricediscount> 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);
var result = categoryProducts.SelectMany(x => x.productPrices)
.SelectMany(x => x.priceDiscounts);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.