我想知道如何按月分组一些数据并总结一个字段。 例如,我有一个MyObjects(DateTimeField, AmountField)
的列表。 我想按DateTimeField
分组,但不是整个日期,只是按月份,然后为每个月总计AmountField
。Linq分组日期(月)
我不知道为什么objectgrouped
为空?
IEnumerable<MyObject> objectList= GetMyObject();
var ObjectGrouped = objectList.GroupBy(l => l.ObjectDate.GetValueOrDefault().Month)
.Select(lg =>
new
{
CurrentMonth = lg.Key,
Total = lg.Sum(w => w.ObjectAmount)
});
ObjectDate ObjectAmount
1/1/2013 3.3
3/1/2013 6.9
13/2/2013 5
3/4/2013 3.6
13/4/2013 15.2
非常相似:http://stackoverflow.com/questions/11574474/linq-get-sum-of-data-group-by-date –