我试图对一个列求和,并获取成员的详细信息
我的表格数据是
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)
};
如何读取结果,我的代码是否正确?
最佳答案
像这样的东西会有用的
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);
}
关于c# - 如何在 Entity Framework 中求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14349573/