How to sum a column in an entity structure

I am trying to summarize a column and get member wise details

My table data

id membername cost 1 a 100 2 aa 100 3 a 100 4 aa 0 5 b 100 

In the Entity Framework, I am trying to summarize the cost column and get the result as follows

 membername totalcost a 200 aa 100 b 100 

then i do it

 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 can I read the results and is this code correct?

+4
source share
1 answer

Something like this 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); } 
+10
source

All Articles