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.
1private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
2{
3 object[] lastValues;
4 DataTable newTable;
5 DataRow[] orderedRows;
6
7 if (FieldNames == null || FieldNames.Length == 0)
8 throw new ArgumentNullException("FieldNames");
9
10 lastValues = new object[FieldNames.Length];
11 newTable = new DataTable();
12
13 foreach (string fieldName in FieldNames)
14 newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
15
16 orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));
17
18 foreach (DataRow row in orderedRows)
19 {
20 if (!fieldValuesAreEqual(lastValues, row, FieldNames))
21 {
22 newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
23
24 setLastValues(lastValues, row, FieldNames);
25 }
26 }
27
28 return newTable;
29}
30
31private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
32{
33 bool areEqual = true;
34
35 for (int i = 0; i < fieldNames.Length; i++)
36 {
37 if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
38 {
39 areEqual = false;
40 break;
41 }
42 }
43
44 return areEqual;
45}
46
47private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
48{
49 foreach (string field in fieldNames)
50 newRow[field] = sourceRow[field];
51
52 return newRow;
53}
54
55private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
56{
57 for (int i = 0; i < fieldNames.Length; i++)
58 lastValues[i] = sourceRow[fieldNames[i]];
59}
60
61
Unfortunately I can't distinguish which one comes first so I credit both of them.
Here's the code.
1private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
2{
3 object[] lastValues;
4 DataTable newTable;
5 DataRow[] orderedRows;
6
7 if (FieldNames == null || FieldNames.Length == 0)
8 throw new ArgumentNullException("FieldNames");
9
10 lastValues = new object[FieldNames.Length];
11 newTable = new DataTable();
12
13 foreach (string fieldName in FieldNames)
14 newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
15
16 orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));
17
18 foreach (DataRow row in orderedRows)
19 {
20 if (!fieldValuesAreEqual(lastValues, row, FieldNames))
21 {
22 newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
23
24 setLastValues(lastValues, row, FieldNames);
25 }
26 }
27
28 return newTable;
29}
30
31private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
32{
33 bool areEqual = true;
34
35 for (int i = 0; i < fieldNames.Length; i++)
36 {
37 if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
38 {
39 areEqual = false;
40 break;
41 }
42 }
43
44 return areEqual;
45}
46
47private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
48{
49 foreach (string field in fieldNames)
50 newRow[field] = sourceRow[field];
51
52 return newRow;
53}
54
55private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
56{
57 for (int i = 0; i < fieldNames.Length; i++)
58 lastValues[i] = sourceRow[fieldNames[i]];
59}
60
61
转载于:https://www.cnblogs.com/HenryLiang/archive/2006/09/26/DataTable_Select_Distinct.html
标签:string,Distinct,Citation,FieldNames,Implementation,DataRow,fieldNames,lastValues 来源: https://blog.csdn.net/weixin_30851867/article/details/98049122