C#中同一DataTable中合并相同条件的行数据

时间:2023-01-12 03:39:19

工作中碰到了如题的问题,静下心来研究了一下,如下代码:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Text.RegularExpressions;
using  System.Collections;
using  System.Data;


namespace  ConsoleApplication1
{
    
class  Program
    {
        
public   static  DataTable tabTemp  =   new  DataTable();
        
public   static  DataRow drTemp  =   null ;
        
public   static  DataTable dt  =   new  DataTable();
        
static   void  Main( string [] args)
        {
             dt.Columns.Add(
" a " typeof (String));
            dt.Columns.Add(
" b " typeof (String));
            dt.Columns.Add(
" c " typeof (String));
            dt.Columns.Add(
" d " typeof (String));
           
            DataRow dr 
=   null ;
            dr 
=  dt.NewRow();
            dr[
0 =   " 1 " ;
            dr[
1 =   " 1 " ;
            dr[
2 =   " 1 " ;
            dr[
3 =   " 1 " ;
            dt.Rows.Add(dr);
            dr 
=  dt.NewRow();
            dr[
0 =   " 1 " ;
            dr[
1 =   " 2 " ;
            dr[
2 =   " 2 " ;
            dr[
3 =   " 2 " ;
            dt.Rows.Add(dr);
            dr 
=  dt.NewRow();
            dr[
0 =   " 1 " ;
            dr[
1 =   " 3 " ;
            dr[
2 =   " 3 " ;
            dr[
3 =   " 3 " ;
            dt.Rows.Add(dr);
            dr 
=  dt.NewRow();
            dr[
0 =   " 2 " ;
            dr[
1 =   " 1 " ;
            dr[
2 =   " 1 " ;
            dr[
3 =   " 1 " ;
            dt.Rows.Add(dr);
            dr 
=  dt.NewRow();
            dr[
0 =   " 2 " ;
            dr[
1 =   " 1 " ;
            dr[
2 =   " 1 " ;
            dr[
3 =   " 1 " ;
            dt.Rows.Add(dr);
            dr 
=  dt.NewRow();
            dr[
0 =   " 3 " ;
            dr[
1 =   " 1 " ;
            dr[
2 =   " 1 " ;
            dr[
3 =   " 1 " ;
            dt.Rows.Add(dr);
            Console.WriteLine(dt.Columns[
0 ].ColumnName  +   "   "   +  dt.Columns[ 1 ].ColumnName  +   "   "   +  dt.Columns[ 2 ].ColumnName  +   "   "   +  dt.Columns[ 3 ].ColumnName  +   "   " );
            
for  ( int  l  =   0 ; l  <  dt.Rows.Count; l ++ )
            {
                Console.WriteLine(dt.Rows[l][
0 ].ToString()  +   "   "   +  dt.Rows[l][ 1 ].ToString()  +   "   "   +  dt.Rows[l][ 2 ].ToString()  +   "   "   +  dt.Rows[l][ 3 ].ToString()  +   "   " );
            }
            
for  ( int  j  =   0 ; j  <  dt.Columns.Count; j ++ )
            {
                tabTemp.Columns.Add(dt.Columns[j].ColumnName, dt.Columns[j].DataType);
            }
            FindComValue();
           
            Console.WriteLine(tabTemp.Columns[
0 ].ColumnName  +   "   "   +  tabTemp.Columns[ 1 ].ColumnName  +   "   "   +  tabTemp.Columns[ 2 ].ColumnName  +   "   "   +  tabTemp.Columns[ 3 ].ColumnName  +   "   " );
            
for  ( int  k  =   0 ; k  <  tabTemp.Rows.Count; k ++ )
            {
                Console.WriteLine(tabTemp.Rows[k][
0 ].ToString()  +   "   "   +  tabTemp.Rows[k][ 1 ].ToString()  +   "   "   +  tabTemp.Rows[k][ 2 ].ToString()  +   "   "   +  tabTemp.Rows[k][ 3 ].ToString()  +   "   " );
            }
            Console.Read();

          


        }

        
static   void  FindComValue()
        {
            
if  (dt.Rows.Count  >   0 )
            {
                
string  row1  =  dt.Rows[ 0 ][ 0 ].ToString(); // 取表的第一行第一列
                DataRow[] drr  =  dt.Select( " a= "   +  row1);
                drTemp 
=  tabTemp.NewRow(); // 临时新的行
                 foreach  (DataRow row  in  drr)
                {                    
                    
for  ( int  i  =   0 ; i  <  dt.Columns.Count; i ++ )
                    {
                        
if  (i  !=   0 )
                        {
                            
if  (drTemp[i].ToString().Trim()  ==   "" )
                                drTemp[i] 
=   " 0 " ;
                            drTemp[i] 
=  Convert.ToString(Convert.ToDouble(drTemp[i].ToString())  +  Convert.ToDouble(row[i].ToString())); // 合并每一列的数据
                        }
                        
else
                        {
                            drTemp[i] 
=  row[i].ToString(); // 添加第一列,不需要相加
                        }
                    }                    
                    dt.Rows.Remove(row);
// 清除此类的行
                }
                tabTemp.Rows.Add(drTemp);
// 添加临时新的行数据
                FindComValue();
            }
        }


    }
}