C# DataTable 按条件拆分多个表(DataTable分组)

最近遇到按条件将DataTable分成多个表的需求,在网上找了很久没有现成的方法,现借鉴网友的方法整理成一个实例,以备后忘:

     

  1  /// <summary>
2 /// DataTable分组
3 /// </summary>
4 /// <param name="source">数据源(要拆分的数据)类型转换</param>
5 /// <param name="destination">分组结果</param>
6 /// <param name="groupByFields">分组条件</param>
7 /// <param name="fieldIndex">条件个数</param>
8 /// <param name="schema">数据源(要拆分的数据)</param>
9 public static void GroupDataRows(IEnumerable<DataRow> source, List<DataTable> destination, string[] groupByFields, int fieldIndex, DataTable schema)
10 {
11 if (fieldIndex >= groupByFields.Length || fieldIndex < 0)
12 {
13 DataTable dt = schema.Clone();
14 foreach (DataRow row in source)
15 {
16 DataRow dr = dt.NewRow();
17 dr.ItemArray = row.ItemArray;
18 dt.Rows.Add(dr);
19 }
20 destination.Add(dt);
21 return;
22 }
23
24 var results = source.GroupBy(o => o[groupByFields[fieldIndex]]);
25 foreach (var rows in results)
26 {
27 GroupDataRows(rows, destination, groupByFields, fieldIndex + 1, schema);
28 }
29 fieldIndex++;
30 }
31
32 #region 测试数据
33
34 /// <summary>
35 /// 数据源表结构
36 /// </summary>
37 /// <returns></returns>
38 private DataTable CreateTb()
39 {
40 DataTable tb = new DataTable();
41 tb.Columns.Add("UUID");
42 tb.Columns.Add("PARENT_UUID");
43 tb.Columns.Add("MASTER_DESC1");
44 tb.Columns.Add("MASTER_DESC2");
45 tb.Columns.Add("SUB_DESC1");
46 tb.Columns.Add("SUB_DESC2");
47 tb.Columns.Add("SUB_DESC3");
48 return tb;
49 }
50
51 /// <summary>
52 /// 拆分后主表数据结构
53 /// </summary>
54 /// <returns></returns>
55 private DataTable CreateParentTb()
56 {
57 DataTable tb = new DataTable();
58 tb.Columns.Add("UUID");
59 tb.Columns.Add("PARENT_UUID");
60 tb.Columns.Add("MASTER_DESC1");
61 tb.Columns.Add("MASTER_DESC2");
62 return tb;
63 }
64
65 /// <summary>
66 /// 拆分后子表结构
67 /// </summary>
68 /// <returns></returns>
69 private DataTable CreateSupTb()
70 {
71 DataTable tb = new DataTable();
72 tb.Columns.Add("UUID");
73 tb.Columns.Add("PARENT_UUID");
74 tb.Columns.Add("SUB_DESC1");
75 tb.Columns.Add("SUB_DESC2");
76 tb.Columns.Add("SUB_DESC3");
77 return tb;
78 }
79
80 /// <summary>
81 /// 数据源(待拆分的数据)
82 /// </summary>
83 /// <returns></returns>
84 private DataTable retDt()
85 {
86 DataTable dt = CreateTb();
87 DataRow row = null;
88 for (int i = 0; i < 7; i++)
89 {
90 int j = 0;
91 if (i % 2 == 0)//PARENT_UUID 的值,按PARENT_UUIDsss1、PARENT_UUIDsss2分组
92 j = 0;
93 else
94 j = 1;
95 row = dt.NewRow();
96 row["UUID"] = System.Guid.NewGuid().ToString();
97 row["PARENT_UUID"] = "PARENT_UUID_ " + j.ToString() ;
98 row["MASTER_DESC1"] = "MASTER_DESC1_ " + i.ToString();
99 row["MASTER_DESC2"] = "MASTER_DESC2_ " + i.ToString();
100 row["SUB_DESC1"] = "SUB_DESC1_ " + i.ToString();
101 row["SUB_DESC2"] = "SUB_DESC2_ " + i.ToString();
102 row["SUB_DESC3"] = "SUB_DESC3_ " + i.ToString();
103 dt.Rows.Add(row);
104 }
105 return dt;
106 }
107
108 /// <summary>
109 /// 测试
110 /// </summary>
111 /// <param name="marstData">返回主表</param>
112 /// <param name="subData">返回子表</param>
113 /// <param name="mesg">返回信息</param>
114 public void SeparateDt(ref DataTable marstData ,ref DataTable subData, out string mesg)
115 {
116 mesg = string.Empty;
117 DataTable sourceData = retDt().Copy();
118 DataTable parentDt = CreateParentTb();
119 DataTable subDt = CreateSupTb();
120 string parentUuid = string.Empty;
121 string subUuid = string.Empty;
122
123 try
124 {
125 #region 方法
126 DataTable source = new DataTable();
127 string[] fileds = new string[] { "PARENT_UUID" }; // 分组条件
128 List<DataTable> grouped = new List<DataTable>(); // 存储分组结果
129 source = sourceData;
130 GroupDataRows(source.Rows.Cast<DataRow>(), grouped, fileds, 0, source);
131
132 string mesga = string.Empty;
133 // 输出分组
134 foreach (DataTable dt in grouped)
135 {
136 int i = 0;
137 //处理主表数据
138 DataRow parentRow = parentDt.NewRow();
139 parentUuid = System.Guid.NewGuid().ToString();
140 parentRow["UUID"] = parentUuid;
141 parentRow["PARENT_UUID"] = dt.Rows[i]["PARENT_UUID"].ToString();
142 parentRow["MASTER_DESC1"] = dt.Rows[i]["MASTER_DESC1"].ToString();
143 parentRow["MASTER_DESC2"] = dt.Rows[i]["MASTER_DESC2"].ToString();
144
145 parentDt.Rows.Add(parentRow);
146 //处理子表数据
147 foreach (DataRow row in dt.Rows)
148 {
149 DataRow subRow = subDt.NewRow();
150 subUuid = System.Guid.NewGuid().ToString();
151
152 subRow["UUID"] = subUuid;
153 subRow["PARENT_UUID"] = parentUuid;
154 subRow["SUB_DESC1"] = row["SUB_DESC1"].ToString();
155 subRow["SUB_DESC2"] = row["SUB_DESC2"].ToString(); ;
156 subRow["SUB_DESC3"] = row["SUB_DESC3"].ToString(); ;
157 subDt.Rows.Add(subRow);
158 }
159 }
160 #endregion
161 marstData = parentDt.Copy();
162 subData = subDt.Copy();
163 }
164 catch (Exception ex)
165 {
166 mesg = "程序有问题:" + ex.Message;
167 }
168 }
posted @ 2011-07-31 11:52  无名草  阅读(10920)  评论(0编辑  收藏  举报