how to use count on a groupby using extension methods?
I need to use get count(*) like sql server on linq, how can I use extenstion methods to do that ? here is what I am trying to do
var test = empire.Case_Offence.Join( empire.Offences , w => w.OffenceId ,
x => x.Id ,
( w , x ) => new { w.Id , w.OffenceId , x.Name } )
.GroupBy( ww => new {ww.Name, ww开发者_开发技巧.Id,ww.OffenceId } ) ;
tnx :)
Try:
var test = empire.Case_Offence.Join( empire.Offences , w => w.OffenceId ,
x => x.Id ,
( w , x ) => new { w.Id , w.OffenceId , x.Name } )
.GroupBy( ww => new {ww.Name, ww.Id,ww.OffenceId } )
.Select ( g => new { Key = g.Key, Count = g.Count() } );
test
will then be a set of anonymous types with two parameters - Key
- the group-by key, and Count
- the number of items with that key.
精彩评论