Linq group by and count - how do I add more columns?
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 开发者_JAVA技巧 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?
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)
精彩评论