程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> linq lambda 分組後排序

linq lambda 分組後排序

編輯:C#基礎知識

1.lamdba分組排序
foodBusinessDistrict.
                        GroupBy(x => new
                        {
                            x.CityLocationID,
                            x.CityLocationName,
                            x.BusinessDistrctID,
                            x.BusinessDistrctName
                        })
                        .Select(g=> new BusinessDistrictWithCountModel
                        {
                            CityLocationID = g.Key.CityLocationID,
                            CityLocationName = g.Key.CityLocationName,
                            BusinessDistrctID = g.Key.BusinessDistrctID,
                            BusinessDistrctName = g.Key.BusinessDistrctName,
                            ProductCount = g.Sum(a => a.ProductCount)
                        })
                        .OrderByDescending(x => x.ProductCount)
                        .ToList();


2.group分組
eg1.
var id = (from p in (from ps in GPEcontext.hbl_product_stock
                    group ps by new {ps.ProductID}
                    into G
                    select new
                    {
                        Key = G.Key,
                        Count = G.Count()
                    })
                    orderby p.Count descending
                         select p.Key.ProductID.Value).FirstOrDefault();
eg2.
DataTable dt = (from x in dsResult.Tables[0].AsEnumerable()
                                where DataTrans.CBoolean(x["IsChecked"]) == true
                                group x by new

                                {
                                    no = x.Field<string>("NO"),
                                    ptno = x.Field<string>("PTNO"),
                                    ver = x.Field<int>("VER"),
                                    kd = x.Field<string>("KD"),
                                    que_da = Convert.ToDateTime(x.Field<DateTime>("QUE_DA").ToString("yyyy/MM/dd"))

                                } into g
                                orderby g.Key.no,g.Key.ptno,g.Key.ver,g.Key.kd,g.Key.que_da
                                select new
                                {
                                    qty = g.Sum(x => Convert.ToInt32(x["QUE_QTY"])),
                                  stock=g.Sum(x=>Convert.ToInt32(x["STOCK"])),
                                  no=g.Key.no ,
                                  ptno=g.Key.ptno,
                                  ver=g.Key.ver,
                                  kd=g.Key.kd,
                                  que_da=g.Key.que_da
                                }).ConvertDataTable();

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved