Remember that Linq GroupBy is not like SQL GroupBy. Linq GroupBy returns .Key (which is your conditional group) and then IEnumerable <> of what you are grouping. Therefore, if you want to count all the rows for each binding together with the binding itself, this will be:
var bindingsAndCounts = Db.Table.Where(u => u.UserId == userId && u.Binding == binding) .GroupBy(u => u.Binding) .Select(g => new {g.Key, BindingCount = g.Count()});
This is a much more powerful construct than with SQL, because you can do anything with "g" in the expression.
If you want to list each group, you can omit the choice:
foreach (var group in whateveryourgroupingExpressionWas) { Console.WriteLine(group.Key); foreach (var row in group) { Console.WriteLine("ID: " + row.Id + "Name " + row.Name ...); } }
source share