如何去掉dataset中的重复行

转自风满袖的Blog http://jiezhi.cnblogs.com/archive/2005/01/05/86838.html

  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  构造方法 
 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 私有方法 
 46 
 47        /// <summary>
 48        /// 比较两列
 49        /// </summary>
 50        /// <param name="objectA"></param>
 51        /// <param name="objectB"></param>
 52        /// <returns></returns>

 53        private bool ColumnEqual(object objectA, object objectB) 
 54        
 55            if ( objectA == DBNull.Value && objectB == DBNull.Value ) 
 56            
 57                return true
 58            }
 
 59            if ( objectA == DBNull.Value || objectB == DBNull.Value ) 
 60            
 61                return false
 62            }
 
 63            return ( objectA.Equals( objectB ) ); 
 64        }
 
 65 
 66        /// <summary>
 67        /// 比较两行
 68        /// </summary>
 69        /// <param name="rowA">A表的行</param>
 70        /// <param name="rowB">B表的行</param>
 71        /// <param name="columns">所对应的列</param>
 72        /// <returns></returns>

 73        private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns) 
 74        
 75            bool result = true
 76            for ( int i = 0; i < columns.Count; i++ ) 
 77            
 78                result &= ColumnEqual( rowA[ columns[ i ].ColumnName ], rowB[ columns[ i ].ColumnName ] ); 
 79            }
 
 80            return result; 
 81        }
 
 82 
 83        /// <summary>
 84        /// 暂时不知道
 85        /// </summary>
 86        /// <param name="fieldList"></param>
 87        /// <param name="allowRelation"></param>

 88        private void ParseFieldList(string fieldList, bool allowRelation) 
 89        
 90            if ( m_FieldList == fieldList ) 
 91            
 92                return
 93            }
 
 94            m_FieldInfo = new ArrayList(); 
 95            m_FieldList = fieldList; 
 96            FieldInfo Field; 
 97            string[] FieldParts; 
 98            string[] Fields = fieldList.Split( ',' ); 
 99            for ( int i = 0; i <= Fields.Length - 1; i++ ) 
100            
101                Field = new FieldInfo(); 
102                FieldParts = Fields[ i ].Trim().Split( ' ' ); 
103                switch ( FieldParts.Length ) 
104                
105                    case 1
106                        //to be set at the end of the loop 
107                        break
108                    case 2
109                        Field.FieldAlias = FieldParts[ 1 ]; 
110                        break
111                    default
112                        return
113                }
 
114                FieldParts = FieldParts[ 0 ].Split( '.' ); 
115                switch ( FieldParts.Length ) 
116                
117                    case 1
118                        Field.FieldName = FieldParts[ 0 ]; 
119                        break
120                    case 2
121                        if ( allowRelation == false ) 
122                        
123                            return
124                        }
 
125                        Field.RelationName = FieldParts[ 0 ].Trim(); 
126                        Field.FieldName = FieldParts[ 1 ].Trim(); 
127                        break
128                    default
129                        return
130                }
 
131                if ( Field.FieldAlias == null ) 
132                
133                    Field.FieldAlias = Field.FieldName; 
134                }
 
135                m_FieldInfo.Add( Field ); 
136            }
 
137        }
 
138 
139        /// <summary>
140        /// 创建DataTable
141        /// </summary>
142        /// <param name="tableName">表名</param>
143        /// <param name="sourceTable">源表</param>
144        /// <param name="fieldList"></param>
145        /// <returns></returns>

146        private DataTable CreateTable(string tableName, DataTable sourceTable, string fieldList) 
147        
148            DataTable dt; 
149            if ( fieldList.Trim() == "" ) 
150            
151                dt = sourceTable.Clone(); 
152                dt.TableName = tableName; 
153            }
 
154            else 
155            
156                dt = new DataTable( tableName ); 
157                ParseFieldList( fieldList, false ); 
158                DataColumn dc; 
159                foreach ( FieldInfo Field in m_FieldInfo ) 
160                
161                    dc = sourceTable.Columns[ Field.FieldName ]; 
162                    DataColumn column = new DataColumn(); 
163                    column.ColumnName = Field.FieldAlias; 
164                    column.DataType = dc.DataType; 
165                    column.MaxLength = dc.MaxLength; 
166                    column.Expression = dc.Expression; 
167                    dt.Columns.Add( column ); 
168                }
 
169            }
 
170            if ( ds != null ) 
171            
172                ds.Tables.Add( dt ); 
173            }
 
174            return dt; 
175        }
 
176 
177        /// <summary>
178        /// 插入表
179        /// </summary>
180        /// <param name="destTable">DataTable</param>
181        /// <param name="sourceTable">源DataTable</param>
182        /// <param name="fieldList"></param>
183        /// <param name="rowFilter"></param>
184        /// <param name="sort"></param>

185        private void InsertInto(DataTable destTable, DataTable sourceTable, 
186            string fieldList, string rowFilter, string sort) 
187        
188            ParseFieldList( fieldList, false ); 
189            DataRow[] rows = sourceTable.Select( rowFilter, sort ); 
190            DataRow destRow; 
191            foreach ( DataRow sourceRow in rows ) 
192            
193                destRow = destTable.NewRow(); 
194                if ( fieldList == "" ) 
195                
196                    foreach ( DataColumn dc in destRow.Table.Columns ) 
197                    
198                        if ( dc.Expression == "" ) 
199                        
200                            destRow[ dc ] = sourceRow[ dc.ColumnName ]; 
201                        }
 
202                    }
 
203                }
 
204                else 
205                
206                    foreach ( FieldInfo field in m_FieldInfo ) 
207                    
208                        destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ]; 
209                    }
 
210                }
 
211                destTable.Rows.Add( destRow ); 
212            }
 
213        }
 
214 
215        /// <summary>
216        /// 暂时不知道
217        /// </summary>
218        /// <param name="FieldList"></param>

219        private void ParseGroupByFieldList(string FieldList) 
220        
221            if ( GroupByFieldList == FieldList ) 
222            
223                return
224            }
 
225            GroupByFieldInfo = new ArrayList(); 
226            FieldInfo Field; 
227            string[] FieldParts; 
228            string[] Fields = FieldList.Split( ',' ); 
229            for ( int i = 0; i <= Fields.Length - 1; i++ ) 
230            
231                Field = new FieldInfo(); 
232                FieldParts = Fields[ i ].Trim().Split( ' ' ); 
233                switch ( FieldParts.Length ) 
234                
235                    case 1
236                        //to be set at the end of the loop 
237                        break
238                    case 2
239                        Field.FieldAlias = FieldParts[ 1 ]; 
240                        break
241                    default
242                        return
243                }
 
244 
245                FieldParts = FieldParts[ 0 ].Split( '(' ); 
246                switch ( FieldParts.Length ) 
247                
248                    case 1
249                        Field.FieldName = FieldParts[ 0 ]; 
250                        break
251                    case 2
252                        Field.Aggregate = FieldParts[ 0 ].Trim().ToLower(); 
253                        Field.FieldName = FieldParts[ 1 ].Trim( ' '')' ); 
254                        break
255                    default
256                        return
257                }
 
258                if ( Field.FieldAlias == null ) 
259                
260                    if ( Field.Aggregate == null ) 
261                    
262                        Field.FieldAlias = Field.FieldName; 
263                    }
 
264                    else 
265                    
266                        Field.FieldAlias = Field.Aggregate + "of" + Field.FieldName; 
267                    }
 
268                }
 
269                GroupByFieldInfo.Add( Field ); 
270            }
 
271            GroupByFieldList = FieldList; 
272        }
 
273 
274        /// <summary>
275        /// 创建一个分组DataTable
276        /// </summary>
277        /// <param name="tableName">表名</param>
278        /// <param name="sourceTable">DataTable</param>
279        /// <param name="fieldList">分组字段</param>
280        /// <returns></returns>

281        private DataTable CreateGroupByTable(string tableName, DataTable sourceTable, string fieldList) 
282        
283            if ( fieldList == null || fieldList.Length == 0 ) 
284            
285                return sourceTable.Clone(); 
286            }
 
287            else 
288            
289                DataTable dt = new DataTable( tableName ); 
290                ParseGroupByFieldList( fieldList ); 
291                foreach ( FieldInfo Field in GroupByFieldInfo ) 
292                
293                    DataColumn dc = sourceTable.Columns[ Field.FieldName ]; 
294                    if ( Field.Aggregate == null ) 
295                    
296                        dt.Columns.Add( Field.FieldAlias, dc.DataType, dc.Expression ); 
297                    }
 
298                    else 
299                    
300                        dt.Columns.Add( Field.FieldAlias, dc.DataType ); 
301                    }
 
302                }
 
303                if ( ds != null ) 
304                
305                    ds.Tables.Add( dt ); 
306                }
 
307                return dt; 
308            }
 
309        }
 
310 
311        private void InsertGroupByInto(DataTable destTable, DataTable sourceTable, string fieldList, 
312            string rowFilter, string groupBy) 
313        
314            if ( fieldList == null || fieldList.Length == 0 ) 
315            
316                return
317            }
 
318            ParseGroupByFieldList( fieldList );  
319            ParseFieldList( groupBy, false );  
320            DataRow[] rows = sourceTable.Select( rowFilter, groupBy ); 
321            DataRow lastSourceRow = null, destRow = null
322            bool sameRow; 
323            int rowCount = 0
324            foreach ( DataRow sourceRow in rows ) 
325            
326                sameRow = false
327                if ( lastSourceRow != null ) 
328                
329                    sameRow = true
330                    foreach ( FieldInfo Field in m_FieldInfo ) 
331                    
332                        if ( !ColumnEqual( lastSourceRow[ Field.FieldName ], sourceRow[ Field.FieldName ] ) ) 
333                        
334                            sameRow = false
335                            break
336                        }
 
337                    }
 
338                    if ( !sameRow ) 
339                    
340                        destTable.Rows.Add( destRow ); 
341                    }
 
342                }
 
343                if ( !sameRow ) 
344                
345                    destRow = destTable.NewRow(); 
346                    rowCount = 0
347                }
 
348                rowCount += 1
349                foreach ( FieldInfo field in GroupByFieldInfo ) 
350                
351                    switch ( field.Aggregate.ToLower() ) 
352                    
353                        case null:  
354                        case ""
355                        case "last"
356                            destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ]; 
357                            break
358                        case "first"
359                            if ( rowCount == 1 ) 
360                            
361                                destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ]; 
362                            }
 
363                            break
364                        case "count"
365                            destRow[ field.FieldAlias ] = rowCount; 
366                            break
367                        case "sum"
368                            destRow[ field.FieldAlias ] = Add( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] ); 
369                            break
370                        case "max"
371                            destRow[ field.FieldAlias ] = Max( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] ); 
372                            break
373                        case "min"
374                            if ( rowCount == 1 ) 
375                            
376                                destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ]; 
377                            }
 
378                            else 
379                            
380                                destRow[ field.FieldAlias ] = Min( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] ); 
381                            }
 
382                            break
383                    }
 
384                }
 
385                lastSourceRow = sourceRow; 
386            }
 
387            if ( destRow != null ) 
388            
389                destTable.Rows.Add( destRow ); 
390            }
 
391        }
 
392 
393        private object Min(object a, object b) 
394        
395            if ( ( a is DBNull ) || ( b is DBNull ) ) 
396            
397                return DBNull.Value; 
398            }
 
399            if ( ( (IComparable) a ).CompareTo( b ) == -1 ) 
400            
401                return a; 
402            }
 
403            else 
404            
405                return b; 
406            }
 
407        }
 
408 
409        private object Max(object a, object b) 
410        
411            if ( a is DBNull ) 
412            
413                return b; 
414            }
 
415            if ( b is DBNull ) 
416            
417                return a; 
418            }
 
419            if ( ( (IComparable) a ).CompareTo( b ) == 1 ) 
420            
421                return a; 
422            }
 
423            else 
424            
425                return b; 
426            }
 
427        }
 
428 
429        private object Add(object a, object b) 
430        
431            if ( a is DBNull ) 
432            
433                return b; 
434            }
 
435            if ( b is DBNull ) 
436            
437                return a; 
438            }
 
439            return ( (decimal) a + (decimal) b ); 
440        }
 
441 
442        private DataTable CreateJoinTable(string tableName, DataTable sourceTable, string fieldList) 
443        
444            if ( fieldList == null ) 
445            
446                return sourceTable.Clone(); 
447            }
 
448            else 
449            
450                DataTable dt = new DataTable( tableName ); 
451                ParseFieldList( fieldList, true ); 
452                foreach ( FieldInfo field in m_FieldInfo ) 
453                
454                    if ( field.RelationName == null ) 
455                    
456                        DataColumn dc = sourceTable.Columns[ field.FieldName ]; 
457                        dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression ); 
458                    }
 
459                    else 
460                    
461                        DataColumn dc = sourceTable.ParentRelations[ field.RelationName ].ParentTable.Columns[ field.FieldName ]; 
462                        dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression ); 
463                    }
 
464                }
 
465                if ( ds != null ) 
466                
467                    ds.Tables.Add( dt ); 
468                }
 
469                return dt; 
470            }
 
471        }
 
472 
473        private void InsertJoinInto(DataTable destTable, DataTable sourceTable, 
474            string fieldList, string rowFilter, string sort) 
475        
476            if ( fieldList == null ) 
477            
478                return
479            }
 
480            else 
481            
482                ParseFieldList( fieldList, true ); 
483                DataRow[] Rows = sourceTable.Select( rowFilter, sort ); 
484                foreach ( DataRow SourceRow in Rows ) 
485                
486                    DataRow DestRow = destTable.NewRow(); 
487                    foreach ( FieldInfo Field in m_FieldInfo ) 
488                    
489                        if ( Field.RelationName == null ) 
490                        
491                            DestRow[ Field.FieldName ] = SourceRow[ Field.FieldName ]; 
492                        }
 
493                        else 
494                        
495                            DataRow ParentRow = SourceRow.GetParentRow( Field.RelationName ); 
496                            DestRow[ Field.FieldName ] = ParentRow[ Field.FieldName ]; 
497                        }
 
498                    }
 
499                    destTable.Rows.Add( DestRow ); 
500                }
 
501            }
 
502        }
 
503 
504        #endregion
 
505 
506        SelectDistinct / Distinct 
640 
641        Select Table Into 
662 
663        Group By Table 
674 
675        Join Tables 
685 
686        Create Table 
747    }

748}

749     

 2.

关于DataSetHelper类(从微软MSDN整理出来)

public class DataSetHelper
 {
  public DataSet ds;
  private System.Collections.ArrayList m_FieldInfo;
  private string m_FieldList;
  private System.Collections.ArrayList GroupByFieldInfo;
  private string GroupByFieldList;

  public DataSetHelper(ref DataSet DataSet)
  {
   ds = DataSet;
  }
  public DataSetHelper()
  {
   ds = null;
  }

  private class FieldInfo
  {
   public string RelationName;
   public string FieldName; //source table field name
   public string FieldAlias; //destination table field name
   public string Aggregate;
  }

  private bool ColumnEqual(object a, object b)
  {
 
   // Compares two values to see if they are equal. Also compares DBNULL.Value.
   // Note: If your DataTable contains object fields, then you must extend this
   // function to handle them in a meaningful way if you intend to group on them.
   
   if ( a == DBNull.Value && b == DBNull.Value ) //  both are DBNull.Value
    return true;
   if ( a == DBNull.Value || b == DBNull.Value ) //  only one is DBNull.Value
    return false;
   return ( a.Equals(b) );  // value type standard comparison
  }

  /// <summary>
  /// 不重复记录的DataTable
  /// </summary>
  /// <param name="TableName">新表名</param>
  /// <param name="SourceTable">源数据表</param>
  /// <param name="FieldName">消除不重复的字段</param>
  /// <returns>DataTable</returns>
  public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
  { 
   DataTable dt = new DataTable(TableName);
   dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
   
   object LastValue = null;
   foreach (DataRow dr in SourceTable.Select("", FieldName))
   {
    if (  LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) )
    {
     LastValue = dr[FieldName];
     dt.Rows.Add(new object[]{LastValue});
    }
   }
   if (ds != null)
    ds.Tables.Add(dt);
   return dt;
  }

  public DataTable SelectDistinct( DataTable SourceTable, string FieldName)
  { 
   return SelectDistinct( "table1", SourceTable, FieldName );
  }


  private void ParseFieldList(string FieldList, bool AllowRelation)
  {
   /*
    * This code parses FieldList into FieldInfo objects  and then
    * adds them to the m_FieldInfo private member
    *
    * FieldList systax:  [relationname.]fieldname[ alias], ...
   */
   if (m_FieldList == FieldList) return;
   m_FieldInfo = new System.Collections.ArrayList();
   m_FieldList = FieldList;
   FieldInfo Field; string[] FieldParts; string[] Fields=FieldList.Split(',');
   int i;
   for (i=0; i<=Fields.Length-1; i++)
   {
    Field=new FieldInfo();
    //parse FieldAlias
    FieldParts = Fields[i].Trim().Split(' ');
    switch (FieldParts.Length)
    {
     case 1:
      //to be set at the end of the loop
      break;
     case 2:
      Field.FieldAlias=FieldParts[1];
      break;
     default:
      throw new Exception("Too many spaces in field definition: '" + Fields[i] + "'.");
    }
    //parse FieldName and RelationName
    FieldParts = FieldParts[0].Split('.');
    switch (FieldParts.Length)
    {
     case 1:
      Field.FieldName=FieldParts[0];
      break;
     case 2:
      if (AllowRelation==false)
       throw new Exception("Relation specifiers not permitted in field list: '" + Fields[i] + "'.");
      Field.RelationName = FieldParts[0].Trim();
      Field.FieldName=FieldParts[1].Trim();
      break;
     default:
      throw new Exception("Invalid field definition: " + Fields[i] + "'.");
    }
    if (Field.FieldAlias==null)
     Field.FieldAlias = Field.FieldName;
    m_FieldInfo.Add (Field);
   }
  }

  private void ParseGroupByFieldList(string FieldList)
  {
   /*
   * Parses FieldList into FieldInfo objects and adds them to the GroupByFieldInfo private member
   *
   * FieldList syntax: fieldname[ alias]|operatorname(fieldname)[ alias],...
   *
   * Supported Operators: count,sum,max,min,first,last
   */
   if (GroupByFieldList == FieldList) return;
   GroupByFieldInfo = new System.Collections.ArrayList();
   FieldInfo Field; string[] FieldParts; string[] Fields = FieldList.Split(',');
   for (int i=0; i<=Fields.Length-1;i++)
   {
    Field = new FieldInfo();
    //Parse FieldAlias
    FieldParts = Fields[i].Trim().Split(' ');
    switch (FieldParts.Length)
    {
     case 1:
      //to be set at the end of the loop
      break;
     case 2:
      Field.FieldAlias = FieldParts[1];
      break;
     default:
      throw new ArgumentException("Too many spaces in field definition: '" + Fields[i] + "'.");
    }
    //Parse FieldName and Aggregate
    FieldParts = FieldParts[0].Split('(');
    switch (FieldParts.Length)
    {
     case 1:
      Field.FieldName = FieldParts[0];
      break;
     case 2:
      Field.Aggregate = FieldParts[0].Trim().ToLower();    //we're doing a case-sensitive comparison later
      Field.FieldName = FieldParts[1].Trim(' ', ')');
      break;
     default:
      throw new ArgumentException("Invalid field definition: '" + Fields[i] + "'.");
    }
    if (Field.FieldAlias==null)
    {
     if (Field.Aggregate==null)
      Field.FieldAlias=Field.FieldName;
     else
      Field.FieldAlias = Field.Aggregate + "of" + Field.FieldName;
    }
    GroupByFieldInfo.Add(Field);
   }
   GroupByFieldList = FieldList;
  }


  public DataTable CreateGroupByTable(string TableName, DataTable SourceTable, string FieldList)
  {
   /*
    * Creates a table based on aggregates of fields of another table
    *
    * RowFilter affects rows before GroupBy operation. No "Having" support
    * though this can be emulated by subsequent filtering of the table that results
    *
    *  FieldList syntax: fieldname[ alias]|aggregatefunction(fieldname)[ alias], ...
   */
   if (FieldList == null)
   {
    throw new ArgumentException("You must specify at least one field in the field list.");
    //return CreateTable(TableName, SourceTable);
   }
   else
   {
    DataTable dt = new DataTable(TableName);
    ParseGroupByFieldList(FieldList);
    foreach (FieldInfo Field in GroupByFieldInfo)
    {
     DataColumn dc  = SourceTable.Columns[Field.FieldName];
     if (Field.Aggregate==null)
      dt.Columns.Add(Field.FieldAlias, dc.DataType, dc.Expression);
     else
      dt.Columns.Add(Field.FieldAlias, dc.DataType);
    }
    if (ds != null) 
     ds.Tables.Add(dt);
    return dt;
   }
  }


  public void InsertGroupByInto(DataTable DestTable, DataTable SourceTable, string FieldList,
   string RowFilter, string GroupBy)
  {
   /*
    * Copies the selected rows and columns from SourceTable and inserts them into DestTable
    * FieldList has same format as CreateGroupByTable
   */
   if (FieldList == null)
    throw new ArgumentException("You must specify at least one field in the field list.");
   ParseGroupByFieldList(FieldList); //parse field list
   ParseFieldList(GroupBy,false);   //parse field names to Group By into an arraylist
   DataRow[] Rows = SourceTable.Select(RowFilter, GroupBy);
   DataRow LastSourceRow = null, DestRow = null; bool SameRow; int RowCount=0;
   foreach(DataRow SourceRow in Rows)
   {
    SameRow=false;
    if (LastSourceRow!=null)
    {
     SameRow=true;
     foreach(FieldInfo Field in m_FieldInfo)
     {
      if (!ColumnEqual(LastSourceRow[Field.FieldName], SourceRow[Field.FieldName]))
      {
       SameRow=false;
       break;
      }
     }
     if (!SameRow)
      DestTable.Rows.Add(DestRow);
    }
    if (!SameRow)
    {
     DestRow = DestTable.NewRow();
     RowCount=0;
    }
    RowCount+=1;
    foreach(FieldInfo Field in GroupByFieldInfo)
    {
     switch(Field.Aggregate)    //this test is case-sensitive
     {
      case null:        //implicit last
      case "":        //implicit last
      case "last":
       DestRow[Field.FieldAlias]=SourceRow[Field.FieldName];
       break;
      case "first":
       if (RowCount==1)
        DestRow[Field.FieldAlias]=SourceRow[Field.FieldName];
       break;
      case "count":
       DestRow[Field.FieldAlias]=RowCount;
       break;
      case "sum":
       DestRow[Field.FieldAlias]=Add(DestRow[Field.FieldAlias], SourceRow[Field.FieldName]);
       break;
      case "max":
       DestRow[Field.FieldAlias]=Max(DestRow[Field.FieldAlias], SourceRow[Field.FieldName]);
       break;
      case "min":
       if (RowCount==1)
        DestRow[Field.FieldAlias]=SourceRow[Field.FieldName];
       else
        DestRow[Field.FieldAlias]=Min(DestRow[Field.FieldAlias], SourceRow[Field.FieldName]);
       break;
     }
    }
    LastSourceRow = SourceRow;
   }
   if(DestRow!=null)
    DestTable.Rows.Add(DestRow);
  }


  private FieldInfo LocateFieldInfoByName(System.Collections.ArrayList FieldList, string Name)
  {
   //Looks up a FieldInfo record based on FieldName
   foreach (FieldInfo Field in FieldList)
   {
    if (Field.FieldName==Name)
     return Field;
   }
   return null;
  }

  private object Min(object a, object b)
  {
   //Returns MIN of two values - DBNull is less than all others
   if ((a is DBNull) || (b is DBNull))
    return DBNull.Value;
   if (((IComparable)a).CompareTo(b)==-1)
    return a;
   else
    return b;
  }  

  private object Max(object a, object b)
  {
   //Returns Max of two values - DBNull is less than all others
   if (a is DBNull)
    return b;
   if (b is DBNull)
    return a;
   if (((IComparable)a).CompareTo(b)==1)
    return a;
   else
    return b;
  }  

  private object Add(object a, object b)
  {
   //Adds two values - if one is DBNull, then returns the other
   if (a is DBNull)
    return b;
   if (b is DBNull)
    return a;
   return ((decimal)a + (decimal)b);
  }
 
  public DataTable SelectGroupByInto(string TableName, DataTable SourceTable, string FieldList,
   string RowFilter, string GroupBy)
  {
   /*
    * Selects data from one DataTable to another and performs various aggregate functions
    * along the way. See InsertGroupByInto and ParseGroupByFieldList for supported aggregate functions.
    */
   DataTable dt = CreateGroupByTable(TableName, SourceTable, FieldList);
   InsertGroupByInto(dt, SourceTable, FieldList, RowFilter, GroupBy);
   return dt;
  }
 }

 

3.

1建立一个DataSetHelper类(DataSetHelper.cs)

public class DataSetHelper
{
    public DataSet ds;
    public DataSetHelper(ref DataSet DataSet)
    {
        ds = DataSet;
    }
    public DataSetHelper()
    {
        ds = null;
    }
    private bool ColumnEqual(object A, object B)
    {
        if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
            return true;
        if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
            return false;
        return (A.Equals(B));  // value type standard comparison
    }
    public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
    {
        DataTable dt = new DataTable(TableName);
        dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);

        object LastValue = null;
        foreach (DataRow dr in SourceTable.Select("", FieldName))
        {
            if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
            {
                LastValue = dr[FieldName];
                dt.Rows.Add(new object[] { LastValue });
            }
        }
        if (ds != null)
            ds.Tables.Add(dt);
        return dt;
    }
}2 建立一个Web窗体,在page_load中写下面的代码       DataSet ds;
        DataSetHelper dsHelper;
        ds = new DataSet();
        dsHelper = new DataSetHelper(ref ds);

        // Create source table
        DataTable dt = new DataTable("Orders");
        dt.Columns.Add("EmployeeID", Type.GetType("System.String"));
        dt.Columns.Add("OrderID", Type.GetType("System.Int32"));
        dt.Columns.Add("Amount", Type.GetType("System.Decimal"));

        dt.Rows.Add(new object[] { "Sam", 5, 25.00 });
        dt.Rows.Add(new object[] { "Tom", 7, 50.00 });
        dt.Rows.Add(new object[] { "Sue", 9, 11.00 });
        dt.Rows.Add(new Object[] { "Tom", 12, 7.00 });
        dt.Rows.Add(new Object[] { "Sam", 14, 512.00 });
        dt.Rows.Add(new Object[] { "Sue", 15, 17.00 });
        dt.Rows.Add(new Object[] { "Sue", 22, 2.50 });
        dt.Rows.Add(new object[] { "Tom", 24, 3.00 });
        dt.Rows.Add(new object[] { "Tom", 33, 78.75 });

        ds.Tables.Add(dt);
       DataTable td=dsHelper.SelectDistinct("DistinctEmployees", ds.Tables["Orders"], "EmployeeID");
       this.GridView1.DataSource = td;
       this.GridView1.DataBind();


该文章转载自网络大本营:http://www.xrss.cn/Info/15184.Html

 

4.

/** <summary>
        /// 返回执行Select distinct后的DataTable
        /// </summary>
        /// <param name="SourceTable">源数据表</param>
        /// <param name="FieldNames">字段集</param>
        /// <returns></returns>
        private DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
        {
            object[] lastValues;
            DataTable newTable;
            DataRow[] orderedRows;

            if (FieldNames == null || FieldNames.Length == 0)
                throw new ArgumentNullException("FieldNames");

            lastValues = new object[FieldNames.Length];
            newTable = new DataTable();

            foreach (string fieldName in FieldNames)
                newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

            orderedRows = SourceTable.Select("", string.Join(",", FieldNames));

            foreach (DataRow row in orderedRows)
            {
                if (!fieldValuesAreEqual(lastValues, row, FieldNames))
                {
                    newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

                    setLastValues(lastValues, row, FieldNames);
                }
            }

            return newTable;
        }

        private bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
        {
            bool areEqual = true;

            for (int i = 0; i < fieldNames.Length; i++)
            {
                if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
                {
                    areEqual = false;
                    break;
                }
            }

            return areEqual;
        }

        private DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
        {
            foreach (string field in fieldNames)
                newRow[field] = sourceRow[field];

            return newRow;
        }

        private void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
        {
            for (int i = 0; i < fieldNames.Length; i++)
                lastValues[i] = sourceRow[fieldNames[i]];
        } 
使用方法
DataTable dt=(System.Data.DataTable)this.ViewState["Mydt"];
            string[] fileds={"Filed1","Filed2"};//DISTINCT字段数组
            DataTable newdt=this.SelectDistinct(dt,fileds);//返回过滤后的DataTable

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值