我想知道如何按月分組一些數據並總結一個字段。 例如,我有一個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 –