How can i group by Month and sum same month branches In Linq C# [Resolved]

Posted by Kasani007 under LINQ on 12/20/2016 | Points: 10 | Views : 3728 | Status : [Member] | Replies : 1
Branch Month Amount
Branch1 10 2000
Branch2 11 3000
Branch1 10 3000
Branch2 10 3000
Branch3 12 4000
Branch2 12 5000
Branch1 12 6000

How can i group by Month and sum same month branches In Linq C#

the output should be like


Branch1 10 5000
Branch2 10 3000
Branch2 11 3000
Branch1 12 6000
Branch2 12 5000
Branch3 12 4000




Responses

Posted by: Kasani007 on: 12/20/2016 [Member] Starter | Points: 25

Up
0
Down

Resolved
let us assume object name with data is listDetails

(from item in listDetails

group item by new{item.Branch.BranchId,item.Month.MonthId} into grouping
select new ListDetailsBE()
{
ListId =grouping.FirstOrDefault().ListId,
Month = grouping.FirstOrDefault().Month.MonthName,
MonthId = grouping.Key.MonthId,
Amount = grouping.Sum(x=>x.Amount),
BranchId = grouping.Key.BranchId,
BranchName = grouping.FirstOrDefault().Branch.BranchName
}).ToList();


Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response