我正在與Linq的GroupBy廝殺。我想我不能用一種表達方式做,但不知道如何解決問題。GroupBy與Linq到對象
我有以下2類:
public class Shipment {
public string PortOfOrigin{get;set;}
public string PortOfDestination{get;set;}
public ICollection<Invoice> Invoices{get;set;}
}
public class Invoice{
public string InvoicePeriod {get;set;}
public decimal Amount {get;set;}
}
我有貨,所有有發票的集合的集合。這是一個例子。
List<Shipment> shipments = new List<Shipment>
{
new Shipment { PortOfOrigin = "USLOS", PortOfDestionation = "UKLON",
Invoices = new List<Invoice>
{
new Invoice{InvoicePeriod = "201106", Amount = 1000},
new Invoice{InvoicePeriod = "201106", Amount = 2000},
new Invoice{InvoicePeriod = "201107", Amount = 1000}
}
},
new Shipment { PortOfOrigin = "USLOS", PortOfDestionation = "UKLON",
Invoices = new List<Invoice>
{
new Invoice{InvoicePeriod = "201106", Amount = 3000},
new Invoice{InvoicePeriod = "201107", Amount = 2000}
}
},
new Shipment { PortOfOrigin = "USDAL", PortOfDestionation = "UKLON",
Invoices = new List<Invoice>
{
new Invoice{InvoicePeriod = "201106", Amount = 3000}
}
}
};
現在我想自由以下:
Shipment.PortOfOrigin,Shipment.PortOfDestionation,Shipment.Invoices.InvoicePeriod。
所以,我想這樣的
PortOfOrigin PortOfDestination InvoicePeriod Amount
------------------------------------------------------------------------
USLOS UKLON 201106 6000
USLOS UKLON 201107 3000
USDAL UKLON 201106 3000
結果是這樣的,甚至可以做到一組這樣的,我想組在Invoices.InvoicePeriod?
對於示例數據和所需結果的示例+1。很難得到一個清晰明確的問題:-) –
謝謝你。 –