【问题标题】:LINQ query for this scenario此方案的 LINQ 查询
【发布时间】:2014-04-01 19:25:51
【问题描述】:

我有这两个接口:

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
                      ???????

【问题讨论】:

    标签: c# linq c#-4.0 ienumerable ilist


    【解决方案1】:

    您可以使用SelectMany 来展平您的内容,然后只需使用Select 并为每个IShippedItem 获取ProductCode

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

    【讨论】:

      【解决方案2】:

      我的错,我提交的早,这里是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多