[英]How sum a column in Entity Framework using linq and join it with another table
[英]how to sum a column in entity framework
我正在嘗試總結一個列並明智地獲取詳細信息
我的表數據是
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.