LINQ查询此方案

Gil*_*lla 2 c# linq ienumerable ilist c#-4.0

我有这两个接口:

public interface IShipment
{
    IEnumerable<IShippedItem> Contents { get; }
    string InvoiceNumber { get; }
}

public interface IShippedItem
{
    string ProductCode { get; }
    int Quantity { get; }
}
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试写一个LINQ声明,让我只是一个列表ProductCode.

怎么去这个?

IEnumerable<IShipment> shipments = GetShipments();
var productCodeList = from shipment in shipment
                      ???????
Run Code Online (Sandbox Code Playgroud)

Sel*_*enç 5

您可以使用SelectMany扁平化内容然后使用Select并获取ProductCode每个内容IShippedItem.

var productCodes = shipments
                   .SelectMany(x => x.Contents)
                   .Select(x => x.ProductCode)
                   .ToList();
Run Code Online (Sandbox Code Playgroud)