我有以下 2 个接口:
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