C# 使用 LINQ 按多个属性分组并求和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15605468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using LINQ to group by multiple properties and sum
提问by Steven
I have a collection of items, here it is:
我有一系列物品,这里是:
AgencyID VendorID StateID Amount Fee
1 1 1 20.00 5.00
1 1 1 10.00 2.00
1 1 1 30.00 8.00
2 2 1 20.00 5.00
2 2 1 5.00 5.00
1 1 2 20.00 5.00
2 2 2 20.00 5.00
2 2 2 40.00 9.00
1 2 2 35.00 6.00
1 2 2 12.00 3.00
I'd like these items to be grouped based on the AgencyID, VendorID, and StateID, and the Total calculated from Amount and Fee (Amount + Fee)
我希望根据 AgencyID、VendorID 和 StateID 以及根据金额和费用(金额 + 费用)计算出的总金额对这些项目进行分组
So using the data above, I'd like to have these results:
所以使用上面的数据,我想得到这些结果:
AgencyID VendorID StateID Total
1 1 1 75.00
2 2 1 35.00
1 1 2 25.00
2 2 2 74.00
1 2 2 56.00
Here's all I have right now, which just gets every row in the database:
这是我现在所拥有的,它只是获取数据库中的每一行:
var agencyContracts = _agencyContractsRepository.AgencyContracts.
Select(ac => new AgencyContractViewModel
{
AgencyContractId = ac.AgencyContractID,
AgencyId = ac.AgencyID,
VendorId = ac.VendorID,
RegionId = ac.RegionID,
Amount = ac.Amount,
Fee = ac.Fee
});
Does anyone know how I can filter and group this with LINQ?
有谁知道我如何用 LINQ 过滤和分组?
采纳答案by Linus Caldwell
Use the .Select()
after grouping:
使用.Select()
后分组:
var agencyContracts = _agencyContractsRepository.AgencyContracts
.GroupBy(ac => new
{
ac.AgencyContractID, // required by your view model. should be omited
// in most cases because group by primary key
// makes no sense.
ac.AgencyID,
ac.VendorID,
ac.RegionID
})
.Select(ac => new AgencyContractViewModel
{
AgencyContractID = ac.Key.AgencyContractID,
AgencyId = ac.Key.AgencyID,
VendorId = ac.Key.VendorID,
RegionId = ac.Key.RegionID,
Amount = ac.Sum(acs => acs.Amount),
Fee = ac.Sum(acs => acs.Fee)
});
回答by Technetium
Linus is spot on in the approach, but a few properties are off. It looks like 'AgencyContractId' is your Primary Key, which is unrelated to the output you want to give the user. I think this is what you want (assuming you change your ViewModel to match the data you say you want in your view).
Linus 在这个方法中表现出色,但有一些属性是关闭的。看起来 'AgencyContractId' 是您的主键,它与您要提供给用户的输出无关。我认为这就是您想要的(假设您更改 ViewModel 以匹配您在视图中所说的数据)。
var agencyContracts = _agencyContractsRepository.AgencyContracts
.GroupBy(ac => new
{
ac.AgencyID,
ac.VendorID,
ac.RegionID
})
.Select(ac => new AgencyContractViewModel
{
AgencyId = ac.Key.AgencyID,
VendorId = ac.Key.VendorID,
RegionId = ac.Key.RegionID,
Total = ac.Sum(acs => acs.Amount) + ac.Sum(acs => acs.Fee)
});