将第一个数据表的行与第二个数据表的列进行比较,并使用匹配的列构建第三个数据表 [英] Compare Row of 1st Datatable with Column of 2nd Datatable and build 3rd datatable with matched columns

查看:111
本文介绍了将第一个数据表的行与第二个数据表的列进行比较,并使用匹配的列构建第三个数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个DataTable - 1.table1

2.table2



这些是我的两个 DATATABLES (视觉c#)



table1



| Par Name | | Par#| - | Units | - | LSL | - | USL | - | SKIP |

扩散| 908513100 | - | 0 | 99.9 | | 0 |

计划| 908514100 | - | 99.5 | 999 | | 0 |

(ADCQ2_P)| 908533100 | - | 1000 | 0 | | 0 |









table2



starttime | (ADCQ2_N)|设备|扩散|程序| (ADCQ2_P)|

11/7/2013 | SAF5100EL | 163 | -0.145712003 | -0.146583006 | 6.29

11/7/2013 | SAF5100EL | 84 | -0.137499005 | -0.137592003 | 6.21

11/7/2013 | SAF5100EL | 44 | -0.142690003 | -0.143250003 | 6.33

11/7/2013 | SAF5100EL | 164 | -0.139434993 | -0.140459001 | 6.23

11/7/2013 | SAF5100EL | 34 | -0.147183999 | -0.148519993 | 6.11





输出应该看起来像

table3



|扩散| |程序| (ADCQ2_P)

-0.145712003 - -0.146583006 - 6.29

-0.137499005 - -0.137592003 - 6.21

-0.142690003 - -0.143250003 - 6.33

-0.139434993 - -0.140459001 - 6.23

-0.147183999 - -0.148519993 - 6.11







这里table2中与table1的行名称匹配的列(例如此处扩散和程序)必须从datatable2获取,并且必须创建新的DataTable3并使用列的排序值(扩散和程序)





我只想从table2中获取那些与table1行匹配的列





使用以下代码构建表格



这就是我构建数据表的方法





使用System;

使用System.Data;

使用Microsoft .VisualBasic.F ileIO;



命名空间ReadDataFromCSVFile

{

静态课程

{

static void Main()

{

string csv_file_path = @C:\ Matlab\Limits.csv;

DataTable table1 = GetDataTabletFromCSVFile(csv_file_path);

Console.WriteLine(Rows count:+ table1.Rows.Count);

Console.ReadLine() ;



}



私有静态DataTable GetDataTabletFromCSVFile(字符串csv_file_path)

{

DataTable table1 = new DataTable(Limits);



使用(TextFieldParser csvReader = new TextFieldParser(csv_file_path))

{

csvReader.SetDelimiters(new string [] {,});

csvReader.HasFieldsEnclosedInQuotes = true;

string [] colFields = csvReader.ReadFields();

foreach(colFields中的字符串列)

{

DataColumn datecolumn = new DataColumn(专栏);

datec olumn.AllowDBNull = true;

table1.Columns.Add(datecolumn);

}

while(!csvReader.EndOfData)

{

string [] fieldData = csvReader.ReadFields();

//将空值设为空

for( int i = 0;我< fieldData.Length; i ++)

{

if(fieldData [i] ==)

{

fieldData [i ] = null;

}

}

table1.Rows.Add(fieldData);



}



}



返回table1;

}











< br $>










添加了代码块 - OriginalGriff [/ edit]

再次添加代码块 - OriginalGriff [/ edit]















table2



I have two DataTable -- 1.table1
2.table2

THESE ARE MY TWO DATATABLES (visual c#)

table1

|Par Name| | Par #|-- |Units |--|LSL|--|USL |--|SKIP |
Diffusion |908513100 |- | 0 | 99.9| |0 |
Program |908514100 |- | 99.5 |999 | |0 |
(ADCQ2_P) |908533100 |- | 1000 | 0 | |0 |




table2

starttime | (ADCQ2_N) | Device | Diffusion | Program | (ADCQ2_P) |
11/7/2013 | SAF5100EL | 163 | -0.145712003 | -0.146583006 | 6.29
11/7/2013 | SAF5100EL | 84 | -0.137499005 | -0.137592003 | 6.21
11/7/2013 | SAF5100EL | 44 | -0.142690003 | -0.143250003 | 6.33
11/7/2013 | SAF5100EL | 164 | -0.139434993 | -0.140459001 | 6.23
11/7/2013 | SAF5100EL | 34 | -0.147183999 | -0.148519993 | 6.11


output should look like
table3

|Diffusion| | Program | (ADCQ2_P)
-0.145712003 - -0.146583006 - 6.29
-0.137499005 - -0.137592003 - 6.21
-0.142690003 - -0.143250003 - 6.33
-0.139434993 - -0.140459001 - 6.23
-0.147183999 - -0.148519993 - 6.11



here the columns in table2 which match the row names of table1(for instance here diffusion and program) have to be fetched from datatable2 and new DataTable3 has to be created and with sorted values of columns (Diffusion and Program)


I want to fetch only those column from table2 into table3 which match with table1 rows


The used the following code to build the tables

This is how I built the Datatable


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);
Console.WriteLine("Rows count:" + table1.Rows.Count);
Console.ReadLine();

}

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 datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
table1.Columns.Add(datecolumn);
}
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);

}

}

return table1;
}











[edit]Code block added - OriginalGriff[/edit]
[edit]Code block added, again - OriginalGriff[/edit]







table2

starttime   |(ADCQ2_N)     | Device   | Diffusion       | Program | 	
11/7/2013    SAF5100EL       163       -0.145712003      -0.146583006   -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产品






output should look like
table3 Product

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









这里table2中的列与table1的行名相匹配(例如这里扩散)和程序)必须从datatable2中获取,并且必须创建新的DataTable3并使用列的排序值(扩散和程序)





我想只将table2中的那些列提取到table3中,与table1行匹配





使用以下代码构建表



这就是我使用System构建数据表的方式





;

使用System.Data;

使用Microsoft.VisualBasic.FileIO;



名称空间ReadDataFromCSVFile

{

静态课程计划

{

static void Main()

{

string csv_file_path = @C:\ Matlab\Limits.csv;

DataTable table1 = GetDataTabletFromCSVFile(csv_file_path);

Console.WriteLine(Rows count:+ table1.Rows.Count);

控制台.ReadLine();



}



私有静态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(colFields中的字符串列)

{

DataColumn datecolumn = new DataColumn(column);

datecolumn.AllowDBNull = true;

table1.Columns.Add(datecolumn);

}

while(!csvReader.End OfData)

{

string [] fieldData = csvReader.ReadFields();

//将空值设为空

for(int i = 0;我< fieldData.Length; i ++)

{

if(fieldData [i] ==)

{

fieldData [i ] = null;

}

}

table1.Rows.Add(fieldData);



}



}



返回table1;

}











< br $>










添加了代码块 - OriginalGriff [/ edit]

[edit]再次添加代码块 - OriginalGriff [/ edit]

推荐答案

我猜动态LINQ是解决方案。检查以下文章。



动态LINQ加入DataTable上的动态列 [ ^ ]



可以在LINQ查询表达式中动态选择字段吗? [ ^ ]



动态linq查询数据集 [ ^ ]
I guess Dynamic LINQ is the solution. Check below articles.

Dynamic LINQ to join on dynamic columns on DataTable[^]

Any way to dynamically choose fields in a LINQ query expression?[^]

dynamic linq query to dataset[^]


这篇关于将第一个数据表的行与第二个数据表的列进行比较,并使用匹配的列构建第三个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆