I am strugling with a GroupBy for Linq. I guess I can't do it in one expression but have no clue how to solve the problem.
I have the following 2 classes:
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;} } I have a collection of Shipments that all have a collection of Invoice. Here is an example.
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} } } }; Now I want to group by the following:
Shipment.PortOfOrigin, Shipment.PortOfDestionation, Shipment.Invoices.InvoicePeriod.
So I want the result like this
PortOfOrigin PortOfDestination InvoicePeriod Amount ------------------------------------------------------------------------ USLOS UKLON 201106 6000 USLOS UKLON 201107 3000 USDAL UKLON 201106 3000 Is this even possible to do a group like this where I want to group on the Invoices.InvoicePeriod?
