我有一个带有类类型TaxDetails的列表
public class TaxDetails
{
public string ID {get; set;}
public string ItemID {get; set;}
public string TaxID {get; set;}
public string TaxCode {get; set;}
public decimal TaxAmount {get; set;}
}可以有许多具有相同税务ID的项目,但是ID不会重复。我需要寄回一本字典,里面有税号和它的总和。这意味着当得到和时,它应该是按税号分组的。
Dictionary<string,decimal> taxSums本词典仅包含列表中该特定的士的税务id及其总税额。
发布于 2016-04-04 15:45:46
这应该是可行的:
var result =
list
.GroupBy(x => x.TaxID) //Group by TaxID
//Convert to dictionary. The key is the TaxID
//and the value is the sum of tax amount values in the individual group
.ToDictionary(g => g.Key, g => g.Sum(x => x.TaxAmount)); 发布于 2016-04-04 15:45:32
然后分组把它们加起来。然后把结果放到字典里。
var taxSums = context.TaxDetails.GroupBy(d => d.TaxID, d => d.TaxAmount)
.ToDictionary(g => g.Key, g => g.Sum());https://stackoverflow.com/questions/36407143
复制相似问题