DataTable.Select("Distinct") Implementation (Citation)

原文链接:http://www.cnblogs.com/HenryLiang/archive/2006/09/26/DataTable_Select_Distinct.html This block of code is to implement a "Select Distinct" data row from a data table.  The origin of this code is from http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx and http://support.microsoft.com/?id=326176
Unfortunately I can't distinguish which one comes first so I credit both of them.

Here's the code.

 1DataTable.Select("Distinct") Implementation (Citation)private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
 2DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation){
 3DataTable.Select("Distinct") Implementation (Citation)     object[] lastValues;
 4DataTable.Select("Distinct") Implementation (Citation)     DataTable newTable;
 5DataTable.Select("Distinct") Implementation (Citation)     DataRow[] orderedRows;
 6DataTable.Select("Distinct") Implementation (Citation)
 7DataTable.Select("Distinct") Implementation (Citation)     if (FieldNames == null || FieldNames.Length == 0)
 8DataTable.Select("Distinct") Implementation (Citation)          throw new ArgumentNullException("FieldNames");
 9DataTable.Select("Distinct") Implementation (Citation)
10DataTable.Select("Distinct") Implementation (Citation)     lastValues = new object[FieldNames.Length];
11DataTable.Select("Distinct") Implementation (Citation)     newTable = new DataTable();
12DataTable.Select("Distinct") Implementation (Citation)
13DataTable.Select("Distinct") Implementation (Citation)     foreach (string fieldName in FieldNames)
14DataTable.Select("Distinct") Implementation (Citation)          newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
15DataTable.Select("Distinct") Implementation (Citation)
16DataTable.Select("Distinct") Implementation (Citation)     orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));
17DataTable.Select("Distinct") Implementation (Citation)
18DataTable.Select("Distinct") Implementation (Citation)     foreach (DataRow row in orderedRows)
19DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)     DataTable.Select("Distinct") Implementation (Citation){
20DataTable.Select("Distinct") Implementation (Citation)          if (!fieldValuesAreEqual(lastValues, row, FieldNames))
21DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)          DataTable.Select("Distinct") Implementation (Citation){
22DataTable.Select("Distinct") Implementation (Citation)               newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
23DataTable.Select("Distinct") Implementation (Citation)
24DataTable.Select("Distinct") Implementation (Citation)               setLastValues(lastValues, row, FieldNames);
25DataTable.Select("Distinct") Implementation (Citation)          }
26DataTable.Select("Distinct") Implementation (Citation)     }
27DataTable.Select("Distinct") Implementation (Citation)
28DataTable.Select("Distinct") Implementation (Citation)     return newTable;
29DataTable.Select("Distinct") Implementation (Citation)}
30DataTable.Select("Distinct") Implementation (Citation)
31DataTable.Select("Distinct") Implementation (Citation)private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
32DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation){
33DataTable.Select("Distinct") Implementation (Citation)     bool areEqual = true;
34DataTable.Select("Distinct") Implementation (Citation)
35DataTable.Select("Distinct") Implementation (Citation)     for (int i = 0; i < fieldNames.Length; i++)
36DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)     DataTable.Select("Distinct") Implementation (Citation){
37DataTable.Select("Distinct") Implementation (Citation)          if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
38DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)          DataTable.Select("Distinct") Implementation (Citation){
39DataTable.Select("Distinct") Implementation (Citation)               areEqual = false;
40DataTable.Select("Distinct") Implementation (Citation)               break;
41DataTable.Select("Distinct") Implementation (Citation)          }
42DataTable.Select("Distinct") Implementation (Citation)     }
43DataTable.Select("Distinct") Implementation (Citation)
44DataTable.Select("Distinct") Implementation (Citation)     return areEqual;
45DataTable.Select("Distinct") Implementation (Citation)}
46DataTable.Select("Distinct") Implementation (Citation)
47DataTable.Select("Distinct") Implementation (Citation)private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
48DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation){
49DataTable.Select("Distinct") Implementation (Citation)     foreach (string field in fieldNames)
50DataTable.Select("Distinct") Implementation (Citation)          newRow[field] = sourceRow[field];
51DataTable.Select("Distinct") Implementation (Citation)
52DataTable.Select("Distinct") Implementation (Citation)     return newRow;
53DataTable.Select("Distinct") Implementation (Citation)}
54DataTable.Select("Distinct") Implementation (Citation)
55DataTable.Select("Distinct") Implementation (Citation)private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
56DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation)DataTable.Select("Distinct") Implementation (Citation){
57DataTable.Select("Distinct") Implementation (Citation)     for (int i = 0; i < fieldNames.Length; i++)
58DataTable.Select("Distinct") Implementation (Citation)          lastValues[i] = sourceRow[fieldNames[i]];
59DataTable.Select("Distinct") Implementation (Citation)
60DataTable.Select("Distinct") Implementation (Citation)
61DataTable.Select("Distinct") Implementation (Citation)

转载于:https://www.cnblogs.com/HenryLiang/archive/2006/09/26/DataTable_Select_Distinct.html

上一篇:Java 根据文件路径解析excel


下一篇:在JAVA实现DataTable对象(二)——DatColumn和DataRow对象实现