此方案的 LINQ 查询



我有以下 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并获取每个IShippedItemProductCode

var productCodes = shipments
                   .SelectMany(x => x.Contents)
                   .Select(x => x.ProductCode)
                   .ToList();

我的错,我早早地屈服了,这里是:

from shipment in shipments 
from shippedItem in shipment.Contents  
select shippedItem.ProductCode

相关内容

  • 没有找到相关文章

最新更新