我已经看过几十个类似的帖子,但我就是无法让它工作。
使用 asp.net MVC 框架,我有一个名为 Contributions 的表,其中包含一个“ContributionDate”列和一个“Amount”列。我正在加载要在图表中显示的日期和金额:
var results = db.Contributions.Where(c => c.Amount > 0);
ArrayList xValue = new ArrayList();
ArrayList yValue = new ArrayList();
results.ToList().ForEach(c => xValue.Add(c.ContributionDate));
results.ToList().ForEach(c => yValue.Add(c.Amount));
以上工作。现在我想总结(即总计)每年的金额。我见过与以下类似的示例,但我显然一无所知(在此示例中,编译器不喜欢 new{} 语句中的“c.ContributionDate”):
var results = db.Contributions
.Where(c => c.Amount > 0)
.GroupBy( c => c.ContributionDate )
.Select(c => new {Amount = c.Sum(b => b.Amount), Date=c.ContributionDate});
谢谢你的帮助!