码迷,mamicode.com
首页 > 其他好文 > 详细

linq中分组查询而且获取每个分组中的第一条记录,数据用于分页绑定

时间:2018-08-18 00:43:45      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:失败   查询   list   个人   字段   logs   check   返回   不规则   

LINQ分组取出第一条数据

Person1: Id=1, Name="Test1"

Person2: Id=1, Name="Test1"

Person3: Id=2, Name="Test2"

以上list如果直接使用distinct方法进行过滤,仍然返回3条数据,而需要的结果是2条数据。下面给出解这个问题的方法:

方法1: Distinct 方法中使用的相等比较器。这个比较器需要重写Equals和GetHashCode方法,个人不推荐,感觉较麻烦,需要些多余的类,并且用起来还要实例化一个比较器,当然自己也可以写一个泛型的比较器生成工厂用来专门生成比较器,但仍然觉得较麻烦。

              MSDN给出的做法,具体参照:http://msdn.microsoft.com/zh-cn/library/bb338049.aspx

方法2:自己扩展一个DistinctBy。这个扩展方法还是很不错的,用起来很简洁,适合为框架添加的Distinct扩展方法。

 

public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)

{

    HashSet<TKey> seenKeys = new HashSet<TKey>();

    foreach (TSource element in source)

    {

        if (seenKeys.Add(keySelector(element)))

        {

            yield return element;

        }

    }

}

 

使用方法如下(针对ID,和Name进行Distinct):

var query = people.DistinctBy(p => new { p.Id, p.Name });

若仅仅针对ID进行distinct:

var query = people.DistinctBy(p => p.Id);

方法3:通过GroupBy分组后,并取出第一条数据。简单易用,很方便。这是一种迂回策略,代码理解起来没有Distinct表意清晰,虽然实现了效果。

 

List<Person> distinctPeople = allPeople

  .GroupBy(p => new {p.Id, p.Name} )

  .Select(g => g.First())

  .ToList();

//这个看起来很美好,但是绑定到gridview时则不显示也不提示错误,采用下面的方法很可靠

ViewState["0964zt"] = "简易报警"; //不显示暂停

        bool xswg = this.chk_xswg.Checked;//是否显示完工

        if (xswg)//显示所有延期的记录,按照ddid降序排列的1000条记录

        {

           

           

            var cx_dd_bj = (from aa in sjklj.sc_ddxx

                  where aa.yq.HasValue && aa.yq.Value > 0 && ((aa.zt.HasValue && aa.zt == false) || aa.zt.HasValue == false)

                  orderby aa.ddid descending

                  select aa).Take(1000).GroupBy(bb => bb.ddid);

            List<sc_ddxx> lst_jybq = new List<sc_ddxx>();

            lst_jybq.Clear();

            foreach(var kk in cx_dd_bj)

            {

                foreach(var gg in kk)

                {

                    lst_jybq.Add(gg);//获取了第一条记录

                    break;

                }

            }

           

           

            this.gv_scjh.DataSource = lst_jybq;

            try

            {

                this.gv_scjh.PageIndex = 0;

            }

            catch(Exception err)

            {

                aspnetajaxjavascript.Msg(this.UpdatePanel1, err.Message, "2", "3000");

            }

            this.gv_scjh.DataBind();

           

        }

        else //不显示完工的,即没有完工的,就是【待定】

        {

 

 

            var cx_dd_bj = (from aa in sjklj.sc_ddxx

                            where aa.czzt == "待定" && aa.yq.HasValue && aa.yq.Value > 0 && ((aa.zt.HasValue && aa.zt == false) || aa.zt.HasValue == false)

                            orderby aa.ddid descending

                            select aa).Take(1000).GroupBy(bb => bb.ddid);

            List<sc_ddxx> lst_jybq = new List<sc_ddxx>();

            lst_jybq.Clear();

            foreach (var kk in cx_dd_bj)

            {

                foreach (var gg in kk)

                {

                    lst_jybq.Add(gg);//获取了第一条记录

                    break;

                }

            }

           

            this.gv_scjh.DataSource = lst_jybq;

            try

            {

                this.gv_scjh.PageIndex = 0;

            }

            catch (Exception err)

            {

                aspnetajaxjavascript.Msg(this.UpdatePanel1, err.Message, "2", "3000");

            }

            this.gv_scjh.DataBind();

           

        }

Linq分组及排序,取前N条记录

2016年05月18日 15:02:10

阅读数:5199

Linq多字段分组排序并取前N条记录时,一定要先分组再排序,不然取到的记录是不规则的

代码示例【按HotWord分组,并取sorNum倒序,取前15条记录】

  1.         [Route("api/XXX/getHotWord")]
  2.         public HttpResponseMessage Post(WordModel model)
  3.         {
  4.             try
  5.             {   
  6.                 using (BZTEntities ctx = new BZTEntities())
  7.                 {                  
  8.                     var wflist = from u in ctx.T_HotWord
  9.                                  where u.Type==model.type                               
  10.                                  group u by new { HotWord = u.HotWord, sortNum = u.SortNum } into g                               
  11.                                  select new { g.Key.HotWord, g.Key.sortNum };
  12.                     wflist = wflist.OrderByDescending(x => x.sortNum);
  13.                     var hotWord = wflist.ToList().Take(15).Select(a => a.HotWord).ToList();
  14.                     var Str = string.Join(",", hotWord);
  15.                     return Request.CreateResponse(HttpStatusCode.OK, new { errorCode = 1, hotWord = Str });
  16.                 }
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 Log.Error("获取xxxxx失败:" + ex.Message, ex);
  21.                 return Request.CreateResponse(HttpStatusCode.OK, new { errorCode = 2 });
  22.             }
  23.         }
  24.     }

特别提醒:list对象绑定gridview时,如果用自带的分页事件绑定,则不能实现分页,下面的代码完美解决。

bool xswg = this.chk_xswg.Checked;//是否显示完工

        if (xswg)//显示所有延期的记录,按照ddid降序排列的1000条记录

        {

 

           

            var cx_dd_bj = (from aa in sjklj.sc_ddxx

                            where aa.yq.HasValue && aa.yq.Value > 0 && ((aa.zt.HasValue && aa.zt == false) || aa.zt.HasValue == false)

                           orderby aa.ddid descending

                           select aa).Take(1000).GroupBy(bb => bb.ddid).OrderByDescending(cc => cc.Key)

                           .Select(dd => dd.FirstOrDefault());

            this.gv_scjh.DataSource = cx_dd_bj;

            this.gv_scjh.DataBind();

     // FirstOrDefault()是解决意外错误的好方法,否则可能没有数据显示甚至没有提示

            /*

            List<sc_ddxx> lst_jybq = new List<sc_ddxx>();

            lst_jybq.Clear();

            foreach(var kk in cx_dd_bj)

            {

                foreach(var gg in kk)

                {

                    lst_jybq.Add(gg);//获取了第一条记录

                    break;

                }

            }

 

            //用户viewstate["datajybj"]记录list数据

            ViewState["datajybj"] = lst_jybq;

           

            this.gv_scjh.DataSource = lst_jybq;

            try

            {

                this.gv_scjh.PageIndex = 0;

            }

            catch(Exception err)

            {

                aspnetajaxjavascript.Msg(this.UpdatePanel1, err.Message, "2", "3000");

            }

            this.gv_scjh.DataBind();

            */

 

        }

        else //不显示完工的,即没有完工的,就是【待定】

        {

 

 

            var cx_dd_bj = (from aa in sjklj.sc_ddxx

                            where aa.czzt == "待定" && aa.yq.HasValue && aa.yq.Value > 0 && ((aa.zt.HasValue && aa.zt == false) || aa.zt.HasValue == false)

                            orderby aa.ddid descending

                            select aa).Take(1000).GroupBy(bb => bb.ddid).OrderByDescending(cc => cc.Key)

                            .Select(dd => dd.FirstOrDefault());

            this.gv_scjh.DataSource = cx_dd_bj;

            this.gv_scjh.DataBind();

            return;

            /*

            List<sc_ddxx> lst_jybq = new List<sc_ddxx>();

            lst_jybq.Clear();

            foreach (var kk in cx_dd_bj)

            {

                foreach (var gg in kk)

                {

                    lst_jybq.Add(gg);//获取了第一条记录

                    break;

                }

            }

 

            //用户viewstate["datajybj"]记录list数据

            ViewState["datajybj"] = lst_jybq;

            this.gv_scjh.DataSource = lst_jybq;

            try

            {

                this.gv_scjh.PageIndex = 0;

            }

            catch (Exception err)

            {

                aspnetajaxjavascript.Msg(this.UpdatePanel1, err.Message, "2", "3000");

            }

            this.gv_scjh.DataBind();

            */

           

        }

linq中分组查询而且获取每个分组中的第一条记录,数据用于分页绑定

标签:失败   查询   list   个人   字段   logs   check   返回   不规则   

原文地址:https://www.cnblogs.com/zrprj/p/9495856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!