且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

构建2个数据表并比较它们并构建数据表3

更新时间:2023-12-01 10:57:16

的问题DataTable table3 = CompareTwoDataTable(DataTable table1,DataTable table2); 

你是否已经在调用 - 剪切和粘贴错误中留下了类型定义?

将其更改为

 DataTable table3 = CompareTwoDataTable(table1,table2); 


引用:

Neewbie,很高兴听到你有结果。



最终工作代码更新如下



  private  静态 DataTable CompareTwoDataTable (DataTable table1,DataTable table2)
{
DataTable table3 = new DataTable() ;
DataRow dr = null ;
string filterExp = string .Empty;
for int i = 0 ; i < table1.Rows.Count; i ++)
{

string col = table1.Rows [i] [ Par Name]。ToString ();
if (table2.Columns.Contains(col))
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof 双跨度>));
filterExp = filterExp + col + asc,;
}

for int j = 0 ; j < table2.Rows.Count; j ++)
{
if (table3.Rows.Count!= table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows [j] [col] =(table2.Rows [j] [col]);
}


}


}

DataTable resultDt = table3.Clone();
for int m = 0 ; m < table3.Columns.Count; m ++)
{
DataView dv = 新的 DataView(table3);
dv.Sort = filterExp.Split(' ,')[m];
table3 = dv.ToTable();
for int i = 0 ; i < table3.Rows.Count; i ++)
{
if (resultDt.Rows.Count!= table3.Rows.Count)
{
resultDt.Rows.Add();
}
resultDt.Rows [i] [m] = table3.Rows [i] [m];
}

}
return resultDt;
}


}
}


引用:

george4986代码正常工作只需要更改的是table3.Columns.Add(col,typeof(double));你是天才男人









Quote:

private static DataTable CompareTwoDataTable(DataTable table1,DataTable table2)

{

DataTable table3 = new DataTable();

DataRow dr = null;

string filterExp = string.Empty;

for(int i = 0; i< table1.Rows.Count; i ++)

{



string col = table1.Rows [i] [Par Name]。ToString();

if(table2.Columns。包含(col))

{

if(!table3.Columns.Contains(col))

{

table3.Columns.Add(col,typeof(double));

filterExp = filterExp + col +asc,;

}



for(int j = 0; j< table2.Rows.Count; j ++)

{

if(table3.Rows.Count!= table2.Rows.Count)

{

dr = table3.NewRow();

table3.Rows.Add(dr);

}

table3.Rows [j] [ col] =(table2.Rows [j] [col]);

}





}




}



DataTable resultDt = table3.Clone();

for(int m = 0; m< table3.Columns.Count; m ++)

{

DataView dv = new DataView(table3) ;

dv.Sort = filterExp.Split(',')[m];

table3 = dv.ToTable();

for (int i = 0; i< table3.Rows.Count; i ++)

{

if(resultDt.Rows.Count!= table3.Rows.Count)

{

resultDt.Rows.Add();

}

resultDt.Rows [i] [m] = table3.Rows [i] [m];

}



}

返回resultDt;

}





}

}


I have built 2 DataTables from CSV files. I have to compare these DataTables (table1 and table2 to built table 3)I have Successfully Built the 2 tables but I don't know how to pass these tables as arguments and build the new table I get error when I try to Build the table3

once table 3 is built I have to sort it from largest to smallest and return the sorted table

Compare Row of 1st Datatable with Column of 2nd Datatable and build 3rd datatable with matched columns[^]

table1

|Par Name.........| Par #|.......Units |.......LSL  |   USL | -----SKIP |
Diffusion.........908513100.......-..........  0  -----99.9--------0 
Program...........908514100.......-.......... 99.5--- 999----------0
name..............901201005.......-..........-0.812----0.1---------1
ADCI1_N[1]........1.0000000.......-..........-0.800----0.1---------1	



table2

starttime   | Product      | Device   | Diffusion       | Program | 
11/7/2013    SAF5100EL       163       -0.145712003      -0.146583006                                 
11/7/2013    SAF5100EL        84       -0.137499005      -0.137592003
11/7/2013    SAF5100EL        44       -0.142690003      -0.143250003  
11/7/2013    SAF5100EL       164       -0.139434993      -0.140459001
11/7/2013    SAF5100EL        34       -0.147183999      -0.148519993



table3

 |Diffusion|       | Program |
-0.145712003      -0.146583006
-0.137499005      -0.137592003
-0.142690003      -0.143250003
-0.139434993      -0.140459001
-0.147183999      -0.148519993




using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;


namespace ReadDataFromCSVFile
{
    static class Program
    {
        static void Main()
        {
            string csv_file_path = @"C:\Matlab\Limits.csv";
            DataTable table1 = GetDataTabletFromCSVFile(csv_file_path);


            string csv_file_path1 = @"C:\Matlab\Sheet1_t168h.csv";
            DataTable table2 = GetDataTabletFromCSVFile1(csv_file_path1);
            

           DataTable table3 = CompareTwoDataTable( table1, table2);


        }
//BUILDING TABLE 1 FROM CSV FILE
        private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable table1 = new DataTable("Limits");

            using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
            {
                csvReader.SetDelimiters(new string[] { "," });
                csvReader.HasFieldsEnclosedInQuotes = true;
                string[] colFields = csvReader.ReadFields();
                foreach (string column in colFields)
                {
                    DataColumn datecolumn1 = new DataColumn(column);
                    datecolumn1.AllowDBNull = true;
                    table1.Columns.Add(datecolumn1);
                }
                while (!csvReader.EndOfData)
                {
                    string[] fieldData = csvReader.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData.Length; i++)
                    {
                        if (fieldData[i] == "")
                        {
                            fieldData[i] = null;
                        }
                    }
                    table1.Rows.Add(fieldData);

                }
        //Skip =1 are deleted and table has only those Par Nam which should be considered  
                DataRow[] rows1;
                rows1 = table1.Select("SKIP = '1'");
                foreach (DataRow r in rows1)
                    r.Delete();
            }

            return table1;
        }

//BUILDING TABLE 2 FROM CSV FILE
        private static DataTable GetDataTabletFromCSVFile1(string csv_file_path1)
        {
            DataTable table2 = new DataTable("Real");

            using (TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1))
            {
                csvReader1.SetDelimiters(new string[] { "," });
                csvReader1.HasFieldsEnclosedInQuotes = true;
                string[] colFields = csvReader1.ReadFields();
                foreach (string column in colFields)
                {
                    DataColumn datecolumn2 = new DataColumn(column);
                    datecolumn2.AllowDBNull = true;
                    table2.Columns.Add(datecolumn2);
                }
                while (!csvReader1.EndOfData)
                {
                    string[] fieldData1 = csvReader1.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData1.Length; i++)
                    {
                        if (fieldData1[i] == "")
                        {
                            fieldData1[i] = null;
                        }
                    }
                    table2.Rows.Add(fieldData1);

                }

            }
            return table2;
        }

//PASSING TABLE 1 AND TABLE 2 TO BUILD TABLE 3
        private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
        {
            DataTable table3 = new DataTable();
            DataRow dr = null;

            for (int i = 0; i < table1.Rows.Count; i++)
            {
                string col = table1.Rows[i]["Par Name"].ToString();
                if (table2.Columns.Contains(col))
                {
                    if (!table3.Columns.Contains(col))
                    {
                        table3.Columns.Add(col, typeof(string));
                    }

                    if (table3.Rows.Count == 0)
                    {
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            dr = table3.NewRow();
                            table3.Rows.Add(dr);
                        }
                    }

                    for (int j = 0; j < table2.Rows.Count; j++)
                    {
                        table3.Rows[j][col] = table2.Rows[j][col].ToString();
                    }
                }


            } 
            return table3;
           }
    
    
    
    
    
    
    
    
    }
}



[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]

The problem with line
DataTable table3 = CompareTwoDataTable(DataTable table1,DataTable table2);

is that you have left the type definitions in the call - cut&paste error perhaps?
Change it to

DataTable table3 = CompareTwoDataTable(table1,table2);


Quote:

Neewbie, happy to hear u have the result.


final working code is updated below

private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{
 
string col = table1.Rows[i]["Par Name"].ToString();
if (table2.Columns.Contains(col))
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(double));
filterExp = filterExp + col + " asc ,";
}
 
for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows[j][col] = (table2.Rows[j][col]);
}
 

}
 

}
 
DataTable resultDt = table3.Clone();
for (int m = 0; m < table3.Columns.Count; m++)
{
DataView dv = new DataView(table3);
dv.Sort = filterExp.Split(',')[m];
table3 = dv.ToTable();
for (int i = 0; i < table3.Rows.Count; i++)
{
if (resultDt.Rows.Count != table3.Rows.Count)
{
resultDt.Rows.Add();
}
resultDt.Rows[i][m] = table3.Rows[i][m];
}
 
}
return resultDt;
}


}
}


Quote:

george4986 the code is working only thing to be changed is table3.Columns.Add(col, typeof(double));You are a Genius man





Quote:

private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{

string col = table1.Rows[i]["Par Name"].ToString();
if (table2.Columns.Contains(col))
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(double));
filterExp = filterExp + col + " asc ,";
}

for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows[j][col] = (table2.Rows[j][col]);
}


}


}

DataTable resultDt = table3.Clone();
for (int m = 0; m < table3.Columns.Count; m++)
{
DataView dv = new DataView(table3);
dv.Sort = filterExp.Split(',')[m];
table3 = dv.ToTable();
for (int i = 0; i < table3.Rows.Count; i++)
{
if (resultDt.Rows.Count != table3.Rows.Count)
{
resultDt.Rows.Add();
}
resultDt.Rows[i][m] = table3.Rows[i][m];
}

}
return resultDt;
}


}
}