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

group by 显示

时间:2019-01-30 12:45:50      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:typeof   sel   int   username   for   sys   format   i++   nbsp   

public static void PrintPersons()         {             //准备数据             DataTable dt = new DataTable();             dt.Columns.Add(new DataColumn("ID", typeof(int)));             dt.Columns.Add(new DataColumn("UserName", typeof(string)));             dt.Columns.Add(new DataColumn("DeptNo", typeof(string)));             dt.Columns.Add(new DataColumn("DeptName", typeof(string)));             DataRow drTemp = null;             for (int i = 10; i <= 18; i++)             {                 drTemp = dt.NewRow();                 drTemp["ID"] = i;                 drTemp["UserName"] = "姓名" + i.ToString();

                if (i < 15)                 {                     drTemp["DeptNo"] = "0001";                     drTemp["DeptName"] = "人事部";                 }                 else                 {                     drTemp["DeptNo"] = "0002";                     drTemp["DeptName"] = "生产部";                 }                 dt.Rows.Add(drTemp);             }

            Console.WriteLine("分组前:");             foreach (DataRow row in dt.Rows)             {                 Console.WriteLine(string.Format("{0} {1} {2} {3} ", row.ItemArray));             }             Console.WriteLine("");

            Console.WriteLine("分组后:");

            //Linq分组查询,并按分组显示人员明细             var query = from g in dt.AsEnumerable()                         group g by new { t1 = g.Field<string>("DeptNo"), t2 = g.Field<string>("DeptName") } into companys                         select new { DeptNo = companys.Key.t1, DeptName = companys.Key.t2, StallInfo = companys };

            foreach (var userInfo in query)             {                 System.Collections.Generic.List<DataRow> dataRows = userInfo.StallInfo.ToList();

                Console.WriteLine(string.Format("{0}({1})人员名单: ", userInfo.DeptName, userInfo.DeptNo));                 foreach (System.Data.DataRow dr in dataRows)                 {                     Console.WriteLine(string.Format("{0} {1} ", dr.ItemArray));                 }             }             Console.ReadLine();         }

group by 显示

标签:typeof   sel   int   username   for   sys   format   i++   nbsp   

原文地址:https://www.cnblogs.com/qiu18359243869/p/10337388.html

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