2014-04-01 48 views
2

我有這2個接口:LINQ查詢此方案

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

回答

5

您可以使用SelectMany扁平化你的內容就用Select並得到ProductCode每個IShippedItem

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