LINQ - Linq code for group by and sum - Asked By MARKAND BHATT on 21-Dec-12 05:41 AM

SELECT   SUM(p.Runs + p.Wickets + p.Catches + p.Runouts) AS points, u.UserId
FROM   tblPlayerUser AS pu INNER JOIN 
        tblUser AS u ON pu.UserId = u.UserId INNER JOIN
            tblPlayer AS p ON pu.PlayerId = p.PlayerId
GROUP BY u.UserId

This is my sql query to fetch the records.
 
I want a code which will perform this query's operation in Linq to Sql(C#)
Can Anyone help me
Suchit shah replied to MARKAND BHATT on 24-Dec-12 02:52 AM
you can be able to right it like this

var result = ( from pu in model.tblplayeruser
                   join u in model.tbluser ON pu.userid  equals  u.userid
                   join p in model.tblplayer ON pu.playerid equals p.playerid
                   select 
                       {
                             Runs = p.runs,
                             Wickets = p.wickets ,
                             Catches = p.catches,
                             Runout = p.Runout
                            UserId = u.userid
                       }
                 group by u.userid).Tolist();


then after when your are fetching the data at that time

string Total = Runs + Wickets + Catches + Runout ;

you will get total there 
meena damwani replied to Suchit shah on 14-Jun-13 01:47 AM
Try this..

var result = ( from pu in model.tblplayeruser
                   join u in model.tbluser ON pu.userid  equals  u.userid
                   join p in model.tblplayer ON pu.playerid equals p.playerid
                   select 
                       {
                            Sum  = p.runs + p.wickets  + p.catches +   p.Runout,
                            UserId = u.userid
                       }
                 group by u.userid).Tolist();


Hope this will help you.