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.
Unfortunately I can't distinguish which one comes first so I credit both of them.
Here's the code.
1
private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
2
{
3
object[] lastValues;
4
DataTable newTable;
5
DataRow[] orderedRows;
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
if (FieldNames == null || FieldNames.Length == 0)
8
throw new ArgumentNullException("FieldNames");
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
lastValues = new object[FieldNames.Length];
11
newTable = new DataTable();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
foreach (string fieldName in FieldNames)
14
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
foreach (DataRow row in orderedRows)
19
{
20
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
21
{
22
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
setLastValues(lastValues, row, FieldNames);
25
}
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
return newTable;
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
32
{
33
bool areEqual = true;
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
return areEqual;
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
48
{
49
foreach (string field in fieldNames)
50
newRow[field] = sourceRow[field];
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
return newRow;
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
private 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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)