How to get sum of data in a list using multiple column values

c#, linq

Solution

I think you are looking to use a group by like this

List<int> ids = new List<int>() { 0, 1, 3, 6 };

filterEntities = (from list in filterEntities 
                  where ids.Contains(list.Id)
                  group list by list.id into g
                  orderby g.Key
                  select new 
                  {
                    ID = g.Key,
                    Age = g.Sum(x => x.Age),
                  }).ToList();

Problem

I have a list using this Linq query ``` filterEntities = (from list in filterEntities where list.Id== 0 && list.Id== 1 && list.Id == 3 && list.Id== 6 select list).OrderBy(r => r.Id).ToList(); ``` Now this linq returns a list like ``` ID Age 0 18 0 19 1 21 3 24 6 32 6 08 ``` I want to generate a list using sum of same Id's which returns like ``` ID Age 0 37 1 21 3 24 6 40 ``` Please suggest me possible query

Original source