[英]LINQ query for this scenario
我有以下兩個接口:
public interface IShipment
{
IEnumerable<IShippedItem> Contents { get; }
string InvoiceNumber { get; }
}
public interface IShippedItem
{
string ProductCode { get; }
int Quantity { get; }
}
現在,我正在嘗試編寫LINQ
語句,以使我僅獲得ProductCode
的列表。
怎么辦呢?
IEnumerable<IShipment> shipments = GetShipments();
var productCodeList = from shipment in shipment
???????
您可以使用SelectMany
展平內容,然后僅使用Select
並獲取每個IShippedItem
的ProductCode
。
var productCodes = shipments
.SelectMany(x => x.Contents)
.Select(x => x.ProductCode)
.ToList();
我的錯,我提早提交,這里是:
from shipment in shipments
from shippedItem in shipment.Contents
select shippedItem.ProductCode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.