如何使用实体框架将相似的记录分组到Linq [英] How To Group Similar Records Using Entity Framework to Linq

查看:70
本文介绍了如何使用实体框架将相似的记录分组到Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.. 

推荐答案

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

Gridrecord.DataSource = groupedCdc;


这篇关于如何使用实体框架将相似的记录分组到Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆