微软知识库里面有个DataSetHelper ,可以对DataSet中的DataTable进行Distinct、Group by、Join和Create。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 using System;
2 using System.Collections;
3 using System.Data;
4
5 namespace Common
6 {
7 /// <summary>
8 /// DataSet助手
9 /// </summary>
10 public class DataSetHelper
11 {
12 private class FieldInfo
13 {
14 public string RelationName;
15 public string FieldName;
16 public string FieldAlias;
17 public string Aggregate;
18 }
19
20 private DataSet ds;
21 private ArrayList m_FieldInfo;
22 private string m_FieldList;
23 private ArrayList GroupByFieldInfo;
24 private string GroupByFieldList;
25
26 public DataSet DataSet
27 {
28 get { return ds; }
29 }
30
31 #region Construction
32
33 public DataSetHelper()
34 {
35 ds = null;
36 }
37
38 public DataSetHelper(ref DataSet dataSet)
39 {
40 ds = dataSet;
41 }
42
43 #endregion
44
45 #region Private Methods
46
47 private bool ColumnEqual(object objectA, object objectB)
48 {
49 if ( objectA == DBNull.Value && objectB == DBNull.Value )
50 {
51 return true;
52 }
53 if ( objectA == DBNull.Value || objectB == DBNull.Value )
54 {
55 return false;
56 }
57 return ( objectA.Equals( objectB ) );
58 }
59
60 private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns)
61 {
62 bool result = true;
63 for ( int i = 0; i < columns.Count; i++ )
64 {
65 result &= ColumnEqual( rowA[ columns[ i ].ColumnName ], rowB[ columns[ i ].ColumnName ] );
66 }
67 return result;
68 }
69
70 private void ParseFieldList(string fieldList, bool allowRelation)
71 {
72 if ( m_FieldList == fieldList )
73 {
74 return;
75 }
76 m_FieldInfo = new ArrayList();
77 m_FieldList = fieldList;
78 FieldInfo Field;
79 string[] FieldParts;
80 string[] Fields = fieldList.Split( ',' );
81 for ( int i = 0; i <= Fields.Length - 1; i++ )
82 {
83 Field = new FieldInfo();
84 FieldParts = Fields[ i ].Trim().Split( ' ' );
85 switch ( FieldParts.Length )
86 {
87 case 1:
88 //to be set at the end of the loop
89 break;
90 case 2:
91 Field.FieldAlias = FieldParts[ 1 ];
92 break;
93 default:
94 return;
95 }
96 FieldParts = FieldParts[ 0 ].Split( '.' );
97 switch ( FieldParts.Length )
98 {
99 case 1:
100 Field.FieldName = FieldParts[ 0 ];
101 break;
102 case 2:
103 if ( allowRelation == false )
104 {
105 return;
106 }
107 Field.RelationName = FieldParts[ 0 ].Trim();
108 Field.FieldName = FieldParts[ 1 ].Trim();
109 break;
110 default:
111 return;
112 }
113 if ( Field.FieldAlias == null )
114 {
115 Field.FieldAlias = Field.FieldName;
116 }
117 m_FieldInfo.Add( Field );
118 }
119 }
120
121 private DataTable CreateTable(string tableName, DataTable sourceTable, string fieldList)
122 {
123 DataTable dt;
124 if ( fieldList.Trim() == "" )
125 {
126 dt = sourceTable.Clone();
127 dt.TableName = tableName;
128 }
129 else
130 {
131 dt = new DataTable( tableName );
132 ParseFieldList( fieldList, false );
133 DataColumn dc;
134 foreach ( FieldInfo Field in m_FieldInfo )
135 {
136 dc = sourceTable.Columns[ Field.FieldName ];
137 DataColumn column = new DataColumn();
138 column.ColumnName = Field.FieldAlias;
139 column.DataType = dc.DataType;
140 column.MaxLength = dc.MaxLength;
141 column.Expression = dc.Expression;
142 dt.Columns.Add( column );
143 }
144 }
145 if ( ds != null )
146 {
147 ds.Tables.Add( dt );
148 }
149 return dt;
150 }
151
152 private void InsertInto(DataTable destTable, DataTable sourceTable,
153 string fieldList, string rowFilter, string sort)
154 {
155 ParseFieldList( fieldList, false );
156 DataRow[] rows = sourceTable.Select( rowFilter, sort );
157 DataRow destRow;
158 foreach ( DataRow sourceRow in rows )
159 {
160 destRow = destTable.NewRow();
161 if ( fieldList == "" )
162 {
163 foreach ( DataColumn dc in destRow.Table.Columns )
164 {
165 if ( dc.Expression == "" )
166 {
167 destRow[ dc ] = sourceRow[ dc.ColumnName ];
168 }
169 }
170 }
171 else
172 {
173 foreach ( FieldInfo field in m_FieldInfo )
174 {
175 destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
176 }
177 }
178 destTable.Rows.Add( destRow );
179 }
180 }
181
182 private void ParseGroupByFieldList(string FieldList)
183 {
184 if ( GroupByFieldList == FieldList )
185 {
186 return;
187 }
188 GroupByFieldInfo = new ArrayList();
189 FieldInfo Field;
190 string[] FieldParts;
191 string[] Fields = FieldList.Split( ',' );
192 for ( int i = 0; i <= Fields.Length - 1; i++ )
193 {
194 Field = new FieldInfo();
195 FieldParts = Fields[ i ].Trim().Split( ' ' );
196 switch ( FieldParts.Length )
197 {
198 case 1:
199 //to be set at the end of the loop
200 break;
201 case 2:
202 Field.FieldAlias = FieldParts[ 1 ];
203 break;
204 default:
205 return;
206 }
207
208 FieldParts = FieldParts[ 0 ].Split( '(' );
209 switch ( FieldParts.Length )
210 {
211 case 1:
212 Field.FieldName = FieldParts[ 0 ];
213 break;
214 case 2:
215 Field.Aggregate = FieldParts[ 0 ].Trim().ToLower();
216 Field.FieldName = FieldParts[ 1 ].Trim( ' ', ')' );
217 break;
218 default:
219 return;
220 }
221 if ( Field.FieldAlias == null )
222 {
223 if ( Field.Aggregate == null )
224 {
225 Field.FieldAlias = Field.FieldName;
226 }
227 else
228 {
229 Field.FieldAlias = Field.Aggregate + "of" + Field.FieldName;
230 }
231 }
232 GroupByFieldInfo.Add( Field );
233 }
234 GroupByFieldList = FieldList;
235 }
236
237 private DataTable CreateGroupByTable(string tableName, DataTable sourceTable, string fieldList)
238 {
239 if ( fieldList == null || fieldList.Length == 0 )
240 {
241 return sourceTable.Clone();
242 }
243 else
244 {
245 DataTable dt = new DataTable( tableName );
246 ParseGroupByFieldList( fieldList );
247 foreach ( FieldInfo Field in GroupByFieldInfo )
248 {
249 DataColumn dc = sourceTable.Columns[ Field.FieldName ];
250 if ( Field.Aggregate == null )
251 {
252 dt.Columns.Add( Field.FieldAlias, dc.DataType, dc.Expression );
253 }
254 else
255 {
256 dt.Columns.Add( Field.FieldAlias, dc.DataType );
257 }
258 }
259 if ( ds != null )
260 {
261 ds.Tables.Add( dt );
262 }
263 return dt;
264 }
265 }
266
267 private void InsertGroupByInto(DataTable destTable, DataTable sourceTable, string fieldList,
268 string rowFilter, string groupBy)
269 {
270 if ( fieldList == null || fieldList.Length == 0 )
271 {
272 return;
273 }
274 ParseGroupByFieldList( fieldList );
275 ParseFieldList( groupBy, false );
276 DataRow[] rows = sourceTable.Select( rowFilter, groupBy );
277 DataRow lastSourceRow = null, destRow = null;
278 bool sameRow;
279 int rowCount = 0;
280 foreach ( DataRow sourceRow in rows )
281 {
282 sameRow = false;
283 if ( lastSourceRow != null )
284 {
285 sameRow = true;
286 foreach ( FieldInfo Field in m_FieldInfo )
287 {
288 if ( !ColumnEqual( lastSourceRow[ Field.FieldName ], sourceRow[ Field.FieldName ] ) )
289 {
290 sameRow = false;
291 break;
292 }
293 }
294 if ( !sameRow )
295 {
296 destTable.Rows.Add( destRow );
297 }
298 }
299 if ( !sameRow )
300 {
301 destRow = destTable.NewRow();
302 rowCount = 0;
303 }
304 rowCount += 1;
305 foreach ( FieldInfo field in GroupByFieldInfo )
306 {
307 switch ( field.Aggregate.ToLower() )
308 {
309 case null:
310 case "":
311 case "last":
312 destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
313 break;
314 case "first":
315 if ( rowCount == 1 )
316 {
317 destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
318 }
319 break;
320 case "count":
321 destRow[ field.FieldAlias ] = rowCount;
322 break;
323 case "sum":
324 destRow[ field.FieldAlias ] = Add( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
325 break;
326 case "max":
327 destRow[ field.FieldAlias ] = Max( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
328 break;
329 case "min":
330 if ( rowCount == 1 )
331 {
332 destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];
333 }
334 else
335 {
336 destRow[ field.FieldAlias ] = Min( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
337 }
338 break;
339 }
340 }
341 lastSourceRow = sourceRow;
342 }
343 if ( destRow != null )
344 {
345 destTable.Rows.Add( destRow );
346 }
347 }
348
349 private object Min(object a, object b)
350 {
351 if ( ( a is DBNull ) || ( b is DBNull ) )
352 {
353 return DBNull.Value;
354 }
355 if ( ( (IComparable) a ).CompareTo( b ) == -1 )
356 {
357 return a;
358 }
359 else
360 {
361 return b;
362 }
363 }
364
365 private object Max(object a, object b)
366 {
367 if ( a is DBNull )
368 {
369 return b;
370 }
371 if ( b is DBNull )
372 {
373 return a;
374 }
375 if ( ( (IComparable) a ).CompareTo( b ) == 1 )
376 {
377 return a;
378 }
379 else
380 {
381 return b;
382 }
383 }
384
385 private object Add(object a, object b)
386 {
387 if ( a is DBNull )
388 {
389 return b;
390 }
391 if ( b is DBNull )
392 {
393 return a;
394 }
395 return ( (decimal) a + (decimal) b );
396 }
397
398 private DataTable CreateJoinTable(string tableName, DataTable sourceTable, string fieldList)
399 {
400 if ( fieldList == null )
401 {
402 return sourceTable.Clone();
403 }
404 else
405 {
406 DataTable dt = new DataTable( tableName );
407 ParseFieldList( fieldList, true );
408 foreach ( FieldInfo field in m_FieldInfo )
409 {
410 if ( field.RelationName == null )
411 {
412 DataColumn dc = sourceTable.Columns[ field.FieldName ];
413 dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );
414 }
415 else
416 {
417 DataColumn dc = sourceTable.ParentRelations[ field.RelationName ].ParentTable.Columns[ field.FieldName ];
418 dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );
419 }
420 }
421 if ( ds != null )
422 {
423 ds.Tables.Add( dt );
424 }
425 return dt;
426 }
427 }
428
429 private void InsertJoinInto(DataTable destTable, DataTable sourceTable,
430 string fieldList, string rowFilter, string sort)
431 {
432 if ( fieldList == null )
433 {
434 return;
435 }
436 else
437 {
438 ParseFieldList( fieldList, true );
439 DataRow[] Rows = sourceTable.Select( rowFilter, sort );
440 foreach ( DataRow SourceRow in Rows )
441 {
442 DataRow DestRow = destTable.NewRow();
443 foreach ( FieldInfo Field in m_FieldInfo )
444 {
445 if ( Field.RelationName == null )
446 {
447 DestRow[ Field.FieldName ] = SourceRow[ Field.FieldName ];
448 }
449 else
450 {
451 DataRow ParentRow = SourceRow.GetParentRow( Field.RelationName );
452 DestRow[ Field.FieldName ] = ParentRow[ Field.FieldName ];
453 }
454 }
455 destTable.Rows.Add( DestRow );
456 }
457 }
458 }
459
460 #endregion
461
462 SelectDistinct / Distinct
463
464 Select Table Into
465
466 Group By Table
467
468 Join Tables
469
470 Create Table
471 }
引用自 :http://www.cnblogs.com/jiezhi/archive/2005/01/05/86838.html