开发者

how to use count on a groupby using extension methods?

开发者 https://www.devze.com 2023-04-12 04:37 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号