user2155362
user2155362

Reputation: 1703

How to select count value by linq

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

Upvotes: 1

Views: 92

Answers (1)

nvoigt
nvoigt

Reputation: 77364

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());
}

Upvotes: 2

Related Questions