其他分享
首页 > 其他分享> > DataTable.Select("Distinct") Implementation (Citation)

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.

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

转载于: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