简体   繁体   English

将第一个数据表的行与第二个数据表的列进行比较,并使用匹配的列构建第三个数据表

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

http://www.codeproject.com/Questions/733071/Compare-Row-of-1st-Datatable-with-Column-of-2nd-Da (Understand the question) THESE ARE MY TWO DATATABLES (visual c#) http://www.codeproject.com/Questions/733071/Compare-Row-of-1st-Datatable-with-Column-of-2nd-Da (理解问题)这些是我的两个数据表(Visual C#)

table1

|Par Name.........| Par #|.......Units |.......LSL  |   USL | -----SKIP |
Diffusion.........908513100.......-..........  0  -----99.9  -----1 
Program...........908514100.......-.........  99.5--- 999    -----





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

output should look like 输出应该看起来像

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

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) 在这里,必须从datatable2中获取与table1的行名匹配的table2中的列(例如,此处的扩散和程序),并且必须创建新的DataTable3并使用列的排序值(Diffusion和Program)

As others have said, this is straight forward logic and that you should try it yourself first. 正如其他人所说,这是直截了当的逻辑,您应该首先自己尝试。 This will give you confidence as well. 这也会给您信心。

Anyways, the following code should do the trick. 无论如何,下面的代码应该可以解决问题。 Tweek it as per your convenience. Tweek,根据您的方便。 Also test it thoroughly. 还要彻底测试。

//dt1 contains Diffusion etc as rows
//dt2 contains diffusion etc as columns
//dt3 is the required table
DataTable dt3 = new DataTable();
DataRow dr = null;

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

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

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较文本文件的第一列、第二列和第三列 - Compare 1st then 2nd then 3rd column of Text File C#-通过将第二个数据表与第三个数据表进行比较,从一个数据表复制行 - C# - Copy rows from one DataTable by comparing with 2nd DataTable to 3rd DataTable 减去第一列和第二列不同的数据,在第三列中显示其答案 - subtract data of different 1st column and 2nd column, showing its answer in 3rd column 至1日、2日、3日、4日(月日) - To 1st, 2nd, 3rd, 4th (Month days) WPF DataGrid-如果该列的绑定数据中的第一,第二或第三最佳值,则对单元格进行着色 - WPF DataGrid - Color cells if 1st, 2nd, or 3rd best value in that column's bound data 用ac#数据表的第一行覆盖列标题 - Override the column header by the 1st row of a c# datatable 将第二行设置为CSV中的标题列以进行数据表转换 - Set 2nd row as header column in CSV to datatable conversion 如何比较2个数据表并在第3个数据表中获得唯一记录? - how to compare 2 datatable and get unique records in 3rd datatable? Unity3D 中的简单记分牌,用于排名和排名第一、第二和第三 - Simple Scoreboard in Unity3D for Ranking and Placement for 1st, 2nd, and 3rd 在第二类中更新第一类的属性并在第三类中获取更新的值 - Update property of 1st class in 2nd class and get updated value in 3rd class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM