There is a SQL query:

Select Organisation, City, SUM(Quantity) as Amount, SUM(Summa) as Summa from C#Table Group by Organisation, City 

Need to do it in LINQ. So far it turned out just something like:

 var query = from data in db.GetTable<TableClass>() group data by data.Organisation into result select new { Name = result.Key, Quantity = result.Sum(i => i.Quantity), Summa = result.Sum(i => i.Summa) }; 
  • what's wrong with the code provided? - Grundy
  • Not enough grouping by City - Timofey Bulankin

1 answer 1

To group by several fields, you need to create an object, MSDN :

 var query = from data in db.GetTable<TableClass>() group data by new { data.Organisation, data.City } into result select new { Name = result.Key.Organisation, City = result.Key.City, Quantity = result.Sum(i => i.Quantity), Summa = result.Sum(i => i.Summa) }; 
  • Thanks It works. - Timofey Bulankin
  • @ TimofeyBulankin, added a link to help - Grundy
  • one
    @ TimofeyBulankin can mark the answer as true (check mark to the left of the answer). - andreycha