datatable dinstinct creat join in group by

    private object Add(object a, object b)
              {
                  if ( a is DBNull )
                  {
                      return b;
                  }
                  if ( b is DBNull )
                  {
                      return a;
                  }
                  return ( (decimal) a + (decimal) b );
              }
      
              private DataTable CreateJoinTable(string tableName, DataTable
      sourceTable, string fieldList)
              {
                  if ( fieldList == null )
                  {
                      return sourceTable.Clone();
                  }
                  else
                  {
                      DataTable dt = new DataTable( tableName );
                      ParseFieldList( fieldList, true );
                      foreach ( FieldInfo field in m_FieldInfo )
                      {
                          if ( field.RelationName == null )
                          {
                              DataColumn dc = sourceTable.Columns[
      field.FieldName ];
                              dt.Columns.Add( dc.ColumnName, dc.DataType,
      dc.Expression );
                          }
                          else
                          {
                              DataColumn dc = sourceTable.ParentRelations[
      field.RelationName ].ParentTable.Columns[ field.FieldName ];
                              dt.Columns.Add( dc.ColumnName, dc.DataType,
      dc.Expression );
                          }
                      }
                      if ( ds != null )
                      {
                          ds.Tables.Add( dt );
                      }
                      return dt;
                  }
              }
      
              private void InsertJoinInto(DataTable destTable, DataTable
      sourceTable,
                                          string fieldList, string rowFilter,
      string sort)
              {
                  if ( fieldList == null )
                  {
                      return;
                  }
                  else
                  {
                      ParseFieldList( fieldList, true );
                      DataRow[] Rows = sourceTable.Select( rowFilter, sort );
                      foreach ( DataRow SourceRow in Rows )
                      {
                          DataRow DestRow = destTable.NewRow();
                          foreach ( FieldInfo Field in m_FieldInfo )
                          {
                              if ( Field.RelationName == null )
                              {
                                  DestRow[ Field.FieldName ] = SourceRow[
      Field.FieldName ];
                              }
                              else
                              {
                                  DataRow ParentRow = SourceRow.GetParentRow(
      Field.RelationName );
                                  DestRow[ Field.FieldName ] = ParentRow[
      Field.FieldName ];
                              }
                          }
                          destTable.Rows.Add( DestRow );
                      }
                  }
              }
      
              #endregion
      
              SelectDistinct / DistinctSelectDistinct / DistinctSelectDistinct /
      Distinct#region SelectDistinct / Distinct
      
              /**//**//**//**//**//**//** <summary>
              /// 按照fieldName从sourceTable中选择出不重复的行,
              /// 相当于select distinct fieldName from sourceTable
              /// </summary>
              /// <param name="tableName">表名</param>
              /// <param name="sourceTable">源DataTable</param>
              /// <param name="fieldName">列名</param>
              /// <returns>一个新的不含重复行的DataTable,列只包括fieldName指明的列</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.Contains( tableName ) )
                  {
                      ds.Tables.Add( dt );
                  }
                  return dt;
              }
      
              /**//**//**//**//**//**//** <summary>
              /// 按照fieldName从sourceTable中选择出不重复的行,
              /// 相当于select distinct fieldName1,fieldName2,,fieldNamen from
      sourceTable
              /// </summary>
              /// <param name="tableName">表名</param>
              /// <param name="sourceTable">源DataTable</param>
              /// <param name="fieldNames">列名数组</param>
              /// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns>
              public DataTable SelectDistinct(string tableName, DataTable
      sourceTable, string[] fieldNames)
              {
                  DataTable dt = new DataTable( tableName );
                  object[] values = new object[fieldNames.Length];
                  string fields = "";
                  for ( int i = 0; i < fieldNames.Length; i++ )
                  {
                      dt.Columns.Add( fieldNames[ i ], sourceTable.Columns[
      fieldNames[ i ] ].DataType );
                      fields += fieldNames[ i ] + ",";
                  }
                  fields = fields.Remove( fields.Length - 1, 1 );
                  DataRow lastRow = null;
                  foreach ( DataRow dr in sourceTable.Select( "", fields ) )
                  {
                      if ( lastRow == null || !( RowEqual( lastRow, dr,
      dt.Columns ) ) )
                      {
                          lastRow = dr;
                          for ( int i = 0; i < fieldNames.Length; i++ )
                          {
                              values[ i ] = dr[ fieldNames[ i ] ];
                          }
                          dt.Rows.Add( values );
                      }
                  }
                  if ( ds != null && !ds.Tables.Contains( tableName ) )
                  {
                      ds.Tables.Add( dt );
                  }
                  return dt;
              }
      
              /**//**//**//**//**//**//** <summary>
              /// 按照fieldName从sourceTable中选择出不重复的行,
              /// 并且包含sourceTable中所有的列。
              /// </summary>
              /// <param name="tableName">表名</param>
              /// <param name="sourceTable">源表</param>
              /// <param name="fieldName">字段</param>
              /// <returns>一个新的不含重复行的DataTable</returns>
              public DataTable Distinct(string tableName, DataTable sourceTable,
      string fieldName)
              {
                  DataTable dt = sourceTable.Clone();
                  dt.TableName = tableName;
      
                  object lastValue = null;
                  foreach ( DataRow dr in sourceTable.Select( "", fieldName ) )
                  {
                      if ( lastValue == null || !( ColumnEqual( lastValue, dr[
      fieldName ] ) ) )
                      {
                          lastValue = dr[ fieldName ];
                          dt.Rows.Add( dr.ItemArray );
                      }
                  }
                  if ( ds != null && !ds.Tables.Contains( tableName ) )
                  {
                      ds.Tables.Add( dt );
                  }
                  return dt;
              } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值