Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
name           total score

John             202
Simeon           303
john             100
simeon           200
kennedy         100
kennedy         200

/*I have so many of those records on my database, i want to querry the record and view them in gridview but i want to group them 
i have this querry 
*/

 SchoolEntitycontext sdc = new SchoolEntitycontext();
            var cdc = from c in sdc.records
                     
                      select new
                      {
                          c.RecordId,
                          c.Name,
                          c.Score
                          

                      };
            if (cdc.Count() > 0)
            {
                Gridrecord.DataSource = cdc;
                Gridrecord.DataBind();
            }
//Using Entity framework to linq it works well to populate the record on a gridview... but i want to group them so that each name will come with the total score and not to be repeating them..
Like Example
name           total score

John             302
Simeon           503
kennedy         300


thanks for your wonderful time.. 
Posted

1 solution

C#
var groupedCdc = sdc.records.GroupBy(c=>c.Name)
                    .Select(g=>new {Name =g.Key, Score =g.Sum(x=>x.Score)}).ToList();

Gridrecord.DataSource = groupedCdc;
 
Share this answer
 
v2
Comments
Member 10835026 24-May-14 20:34pm    
this what i have tried
group c by c.Name into grp
select grp.OrderByDescending(g => new {Name =g.Name, Score =g.score.Sum(x=>x.Score }).FirstOrDefault(); //But i am still having errors.. the major issue is g.score.Sum(x=>x.Score) once i remove it.. it will load properly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900