构建2个数据表并比较它们并构建数据表3 [英] Build 2 datatables and compare them and build datatable 3
问题描述
我已经从CSV文件构建了2个DataTable。我必须比较这些DataTables(table1和table2到构建表3)我已经成功构建了2个表但我不知道如何将这些表作为参数传递并构建新表我在尝试构建table3时遇到错误
一旦构建了表3,我必须将它从最大到最小排序并返回已排序的表格
比较第1个数据表行与第2列数据表并构建具有匹配列的第3个数据表 [ ^ ]
table1
| Par Name ......... | Par#| .......单位| ....... LSL | USL | -----跳过|
Diffusion ......... 908513100 .......- .......... 0 ----- 99.9 -------- 0
节目........... 908514100 .......- .......... 99.5 --- 999 ---------- 0
name .............. 901201005 .......- ..........- 0.812 ---- 0.1 ----- ---- 1
ADCI1_N [1] ........ 1.0000000 .......- ..........- 0.800 ---- 0.1 --- ------ 1
table2
starttime |产品|设备|扩散|程序|
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
|扩散| |程序|
-0。 145712003 -0。 146583006
-0。 137499005 -0。 137592003
-0。 142690003 -0。 143250003
-0。 139434993 -0。 140459001
-0。 147183999 -0。 148519993
使用系统;
使用 System.Data;
使用 Microsoft.VisualBasic.FileIO;
namespace ReadDataFromCSVFile
{
static class 计划
{
静态 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);
}
// 从CSV文件构建表1 / span>
private static DataTable GetDataTabletFromCSVFile( string csv_file_path)
{
DataTable table1 = new DataTable( 限制);
使用(TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters( new string [] { ,});
csvReader.HasFieldsEnclosedInQuotes = true ;
string [] colFields = csvReader.ReadFields();
foreach (字符串列 in colFields)
{
DataColumn datecolumn1 = new DataColumn(column);
datecolumn1.AllowDBNull = true ;
table1.Columns.Add(datecolumn1);
}
while (!csvReader.EndOfData)
{
string [] fieldData = csvReader.ReadFields();
// 将空值设为空
for ( int i = 0 ; i < fieldData.Length; i ++)
{
if (fieldData [i] == )
{
fieldData [i] = 空跨度>;
}
}
table1.Rows.Add(fieldData);
}
// 删除Skip = 1且表只有那些Par Nam应该被视为
DataRow [] rows1;
rows1 = table1.Select( SKIP ='1');
foreach (DataRow r in rows1)
r.Delete();
}
return table1;
}
// 从CSV文件构建表2
private static DataTable GetDataTabletFromCSVFile1( string csv_file_path1)
{
DataTable table2 = new DataTable( 实时跨度>);
使用(TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1))
{
csvReader1.SetDelimiters( new string [] { ,});
csvReader1.HasFieldsEnclosedInQuotes = true ;
string [] colFields = csvReader1.ReadFields();
foreach (字符串列 in colFields)
{
DataColumn datecolumn2 = new DataColumn(column);
datecolumn2.AllowDBNull = true ;
table2.Columns.Add(datecolumn2);
}
while (!csvReader1.EndOfData)
{
string [] fieldData1 = csvReader1.ReadFields();
// 将空值设为空
for ( int i = 0 ; i < fieldData1.Length; i ++)
{
if (fieldData1 [i] == )
{
fieldData1 [i] = 空跨度>;
}
}
table2.Rows.Add(fieldData1);
}
}
return table2;
}
// 通过表1和表2来构建表3
private 静态 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 (串跨度>));
}
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;
}
}
}
删除了SHOUTING,添加了代码块 - OriginalGriff [/ edit]
< blockquote>行
的问题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]);
}
} < br $>
}
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 lineDataTable table3 = CompareTwoDataTable(DataTable table1,DataTable table2);is that you have left the type definitions in the call - cut&paste error perhaps?
Change it toDataTable 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;
}
}
}
这篇关于构建2个数据表并比较它们并构建数据表3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!