【发布时间】:2015-07-06 01:08:08
【问题描述】:
如果我有课:
public class Custom
{
public Custom()
{
}
public DateTime TargetDate { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public decimal Value { get; set; }
}
List<Custom> customItems = new List<Custom>();
上面的列表可以包含任意数量的可以称为相同或不同的项目。这些项目可以在任何一天,甚至在一个特定日期称为相同的多个项目。
我如何使用 linq 按名称和日期对列表进行分组,并计算 sum 为财产价格和average 为财产价值。
所以基本上,结果应该是一个 List> 并且为每个分组名称 + 日期计算的属性。
这是我迄今为止尝试过的。
var aggdata = customItems.GroupBy(t => new { t.Name, t.TargetDate.Date })
.ToDictionary(t => t.Key.Name, t => t.Sum(x => x.Price));
但我缺少字典中的平均值和日期值。
结果应该是这样的:
"TargetDate", "01.01.2015"
"Name", "SomeName"
"Value", "123" // Average of values
"Price", "1234" // Sum of price values
.........
【问题讨论】: