tags:

views:

23

answers:

1

I have the following linq query

from c in AllContracts
group c.StatusId by c.StatusDescription
into g
select new {StatusDescription = g.Key, CountOf = g.Count()}

which returns

StatusDescription       CountOf
End date has passed     1
Suspended               1
Setup phase             2
Running                 7

However I would love to add the column StatusId into the results and also order by StatusId. I have googled around but I am stumped. Can anyone help?

+2  A: 

Try the following:

(from c in AllContracts
group c by new {c.StatusId, c.StatusDescription}
into g
select new {
    StatusId = g.Key.StatusId,
    StatusDescription = g.Key.StatusDescription,
    CountOf = g.Count()
}).OrderBy(item => item.StatusId)
Konamiman
Spot on, could not seem to find that example from Googling!
Rippo
What did you search exactly? You can find this by googling "linq group by two fields" :-)
Konamiman
I kept coming across 101 LINQ Samples, but still could not see the light through the trees! :)
Rippo