Q:

LINQ query with distinct count

I am trying to construct a LINQ query in C# that will give me a list of distinct values from a column in a dataset with a count for each row.   The results would look like this. 
State   Count
AL       55
AK       40
AZ       2

Here is the SQL that does that. 
SELECT name, COUNT(*) AS count
  FROM architecture arch
  GROUP BY name
  ORDER BY name

I've figured out the LINQ to get the DISTINCT values which is. 
var query = ds. Tables[0].AsEnumerable()
    .OrderBy(dr1 => dr1.Field<string>("state"))
    .Select(dr1 => new {state = dr1.Field<string>("state")})
    .Distinct().ToList();

But I can't figure out how to get the COUNT(*) for each distinct value to work in LINQ.   Any idea how I can add that into the LINQ query? 

A:

You need to group your results based on State and the Select count from the group like:
var query = ds. Tables[0].AsEnumerable()
            .GroupBy(r => r.Field<string>("state"))
            .Select(grp => new 
                            { 
                                state = grp. Key, 
                                Count = grp. Count()
                            })
            .OrderBy(o => o.state)
            .ToList();

