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?

you didn't ask as simple and concrete question (e.g.: c# code: List, DataSet, other format? etc.) so it is hard to answer properly. – nozzleman Jan 25, 2017 at 8:11 No need to get mad. I just wanted to point out, that the code you need would look different if you had the data already in a List of some class. Thats not obvious. All we know is that there is data in a database. There are many ways of tackling the issue. All depend on other circumstances. If you expect people to put effort into anwers, you are expected o provide all the information they need to provide a satisfactory answer. i really didn't mean to offend, i just marked another answer adressing the exact problem in a understandable way. – nozzleman Jan 25, 2017 at 8:15 You could of course delete or vote to close your own question, if you feel the duplicate answers your question - which I doubt, at least if you´re unfamiliar with Linq. – MakePeaceGreatAgain Jan 25, 2017 at 8:19
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) });