C# 如何对实体框架中的列求和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14349573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:33:13 来源:igfitidea点击:
how to sum a column in entity framework
提问by Dharmeshsharma
I am trying to sum a column and get details member wise
我正在尝试对一列求和并了解详细信息成员
My table data is
我的表数据是
id membername cost
1 a 100
2 aa 100
3 a 100
4 aa 0
5 b 100
In Entity Framework I try to sum cost column and get result like this
在实体框架中,我尝试对成本列求和并得到这样的结果
membername totalcost
a 200
aa 100
b 100
then I am doing this
然后我在做这个
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)
};
How do I read the results and is my code right or not?
我如何阅读结果,我的代码是否正确?
采纳答案by Vitalik
Something like that will work
像这样的东西会起作用
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);
}