LINQ实现行列转换
LINQ实现行列转换
用SQL语句实现行列转换很容易,但也有时候需要在程序中实现,找了好久,发现一篇文章写的挺不错的
http://blog.csdn.net/smartsmile2012/article/details/8259192
用LINQ实现的行列转换,借鉴了他的办法,修改了下,达到了想要的效果
原始表:

想要达到的效果:

1 private void Form1_Load(object sender, EventArgs e)
2 {
3 //创建表,添加数据作为数据源
4 DataTable dtSource = new DataTable();
5 dtSource.Columns.Add(new DataColumn("Name",typeof(string)));//姓名
6 dtSource.Columns.Add(new DataColumn("DateTime", typeof(string)));//日期
7 dtSource.Columns.Add(new DataColumn("WorkType", typeof(string)));//上班类型
8
9 dtSource.Rows.Add("张三","周一","早班");
10 dtSource.Rows.Add("张三", "周二", "晚班");
11 dtSource.Rows.Add("张三", "周三", "早班");
12 dtSource.Rows.Add("小李", "周二", "早班");
13 dtSource.Rows.Add("小李", "周四", "早班");
14 dtSource.Rows.Add("小李", "周五", "早班");
15 dtSource.Rows.Add("小王", "周一", "晚班");
16 dtSource.Rows.Add("小王", "周三", "晚班");
17 dtSource.Rows.Add("小王", "周五", "晚班");
18
19 gc.DataSource=GetTable(dtSource); //输出转换后的表(这用的是DEV控件的GridView)
20 }
21
22 /// <summary>
23 /// 实现行列转换
24 /// </summary>
25 private DataTable GetTable(DataTable dtSource)
26 {
27 //创建转换后的表,固定列
28 DataTable dt = new DataTable();
29 dt.Columns.Add("NAME", typeof(string)); //名称
30 dt.Columns.Add("WEEK1", typeof(string)); //周一
31 dt.Columns.Add("WEEK2", typeof(string)); //周二
32 dt.Columns.Add("WEEK3", typeof(string)); //周三
33 dt.Columns.Add("WEEK4", typeof(string)); //周四
34 dt.Columns.Add("WEEK5", typeof(string)); //周五
35 string[] times = new string[] { "周一", "周二", "周三", "周四", "周五" };//存放日期作为筛选条件
36
37 //x[0]为NAME,按NAME 分组
38 var data = from x in dtSource.Rows.Cast<DataRow>()
39 group x by x[0] into g
40 select new { Key = g.Key.ToString(), Items = g };
41 data.ToList().ForEach(x =>
42 {
43 string[] array = new string[dt.Columns.Count];
44 array[0] = x.Key;//NAME
45 int m = 0;
46 for (int i = 1; i < dt.Columns.Count; i++)
47 {
48 array[i] = (from y in x.Items
49 where y[1].ToString() == times[m] //y[1]为日期,上面的times为筛选条件
50 select y[2].ToString()).SingleOrDefault();//筛选出来的上班类型
51 m++;
52 };
53 dt.Rows.Add(array);
54 }
55 );
56 return dt;
57 }
输出的效果图:

转自:https://www.cnblogs.com/mengyirensheng/p/3273123.html


浙公网安备 33010602011771号