1

I want express sql below by linq

select catalog,queryname,COUNT(*) from doctemplatecells group by catalog,queryname

I don't know how to get count(*), thanks

user2155362
  • 1,657
  • 5
  • 18
  • 30
  • possible duplicate of [how can i get the count in linq](http://stackoverflow.com/questions/7372805/how-can-i-get-the-count-in-linq) – nvoigt Jun 11 '15 at 09:20
  • http://stackoverflow.com/questions/7372805/how-can-i-get-the-count-in-linq – c4pone Jun 11 '15 at 06:08

1 Answers1

2

Every group consists of it's key (catalog, queryname) and it's elements as represented by the IEnumerable<> implementation of the group.

So if you have a group as a result of LinQ, you can call the extension method Count() on it.

var groups =  doctemplatecells.GroupBy(dtc => new { Catalog = dtc.catalog, QueryName = dtc queryname });

foreach(group in groups)
{
    console.WriteLine("{0} {1} #{2}", group.Key.Catalog, group.Key.QueryName, group.Count());
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142