Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I would like to calculate using linq query the total for every id. I know I have to use the
group by
statement as same in sql:
select id, sum(Value)
from MyTable
group by id
but how can I achieve this using linq or lambda?
–
–
–
from entry in MyTable
group entry by entry.Id into g
select new { Id = g.Key, Sum = g.Sum(e => e.Value) };
Or, if you prefer a method chaining syntax:
MyTable.GroupBy(entry => entry.Id).Select(g => new { Id = g.Key, Sum = g.Sum(e => e.Value) });