使用 Linq to Object 进行 GroupBy
我正在为 Linq 的 GroupBy 苦苦挣扎。我想我无法用一种表达方式做到这一点,但不知道如何解决这个问题。
我有以下 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 上进行分组?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我已更新我的答案以包含发货数量。这要求货件具有 ID 字段。
下面的代码可以解决这个问题:
这是输出:
更新: 正如所承诺的,这是一个完整的工作示例:
UPDATE: I've updated my answer to include the number of Shipments. This requires the shipment to have an ID field.
The following will do the trick:
Here's the output:
UPDATE: As promised, here's a complete working example: