我正在尝试总结一个列并获得明智的成员
我的表数据是
id membername cost
1 a 100
2 aa 100
3 a 100
4 aa 0
5 b 100
在实体框架中,我尝试对成本列求和并得到像这样的结果
membername totalcost
a 200
aa 100
b 100
然后我这样做
var result = from o in db.pruchasemasters.Where(d => d.memberid == d.membertable.id && d.entrydate >= thisMonthStart && d.entrydate <= thisMonthEnd)
group o by new { o.membertable.members } into purchasegroup
select new
{
membername = purchasegroup.Key,
total = purchasegroup.Sum(s => s.price)
};
如何阅读结果并且我的代码是否合适?
答案 0 :(得分:14)
这样的东西会起作用
var result = db.pruchasemasters.GroupBy(o => o.membername)
.Select(g => new { membername = g.Key, total = g.Sum(i => i.cost) });
foreach (var group in result)
{
Console.WriteLine("Membername = {0} Totalcost={1}", group.membername, group.total);
}