简体   繁体   English

如何通过内部联接将两个DataTable联接在一起

[英]How to join two DataTable together with inner join

So I have 2 datatables that I want to merge into 1 in the same way that one merges with an inner join in sql server. 因此,我有2个数据表,我希望将其合并成1个数据表,就像合并一个sql server中的内部联接一样。

One catch is that some of the field names are the same in both tables, but the values may be different (eg. pricing values). 一个陷阱是,两个表中的某些字段名称相同,但是值可能不同(例如,定价值)。 The tables both have a column 'ID' that have the same value and can be joined. 这些表都具有一列“ ID”,它们具有相同的值并且可以联接。

You can do this by, assuming table1 and table2 are your two datatables, and resultTable is the DataTable you wish to populate with the results of the join. 您可以通过以下方式进行操作:假设table1和table2是您的两个数据表,而resultTable是您希望用联接结果填充的DataTable。 Also you can use union all operator. 您也可以使用并集所有运算符。

Write a SQL SELECT statement that references tables in both of the databases, using a JOIN , assign aliases to the tables in the query, and then qualify the column references with the alias. 编写一个SQL SELECT语句,该语句使用JOIN引用两个数据库中的表,为查询中的表分配别名,然后使用别名限定列引用。

And use appropriate expressions in the SELECT list to return the resultset you want to return. 并在SELECT列表中使用适当的表达式返回要返回的结果集。

To emulate a "full outer" join, you may need to use a query with a LEFT JOIN , and another query with an ant-join, and combine the results with a UNION ALL operator. 要模拟“完全外部” LEFT JOIN ,您可能需要使用带有LEFT JOIN查询和另一个带有ant-join的查询,并将结果与UNION ALL运算符组合。

Once you have a SELECT that returns the results you want, you can use that in an INSERT .. SELECT statement to save the results to another table. 一旦有了SELECT返回所需结果的SELECT,就可以在INSERT .. SELECT语句中使用它来将结果保存到另一个表中。


That's about as specific as we can be, without more specific information, beyond a vague description of what you are trying to achieve. 在没有更具体的信息的情况下,这几乎是我们所能做到的具体内容,而不仅仅是对您要实现的目标的模糊描述。

 DataTable dt1 = new DataTable();
    dt1.Columns.Add("CustID", typeof(int));
    dt1.Columns.Add("ColX", typeof(int));
    dt1.Columns.Add("ColY", typeof(int));

    DataTable dt2 = new DataTable();
    dt2.Columns.Add("CustID", typeof(int));
    dt2.Columns.Add("ColZ", typeof(int));

    for (int i = 1; i <= 5; i++)
    {
        DataRow row = dt1.NewRow();
        row["CustID"] = i;
        row["ColX"] = 10 + i;
        row["ColY"] = 20 + i;
        dt1.Rows.Add(row);

        row = dt2.NewRow();
        row["CustID"] = i;
        row["ColZ"] = 30 + i;
        dt2.Rows.Add(row);
    }

    var results = from table1 in dt1.AsEnumerable()
                 join table2 in dt2.AsEnumerable() on (int)table1["CustID"] equals (int)table2["CustID"]
                 select new
                 {
                     CustID = (int)table1["CustID"],
                     ColX = (int)table1["ColX"],
                     ColY = (int)table1["ColY"],
                     ColZ = (int)table2["ColZ"]
                 };
    foreach (var item in results)
    {
        Console.WriteLine(String.Format("ID = {0}, ColX = {1}, ColY = {2}, ColZ = {3}", item.CustID, item.ColX, item.ColY, item.ColZ));
    }
    Console.ReadLine();

// Output:
// ID = 1, ColX = 11, ColY = 21, ColZ = 31
// ID = 2, ColX = 12, ColY = 22, ColZ = 32
// ID = 3, ColX = 13, ColY = 23, ColZ = 33
// ID = 4, ColX = 14, ColY = 24, ColZ = 34
// ID = 5, ColX = 15, ColY = 25, ColZ = 35

Write a SQL SELECT statement that references tables in both of the databases, using a JOIN, assign aliases to the tables in the query, and then qualify the column references with the alias. 编写一条SQL SELECT语句,该语句使用JOIN引用两个数据库中的表,为查询中的表分配别名,然后使用别名限定列引用。

And use appropriate expressions in the SELECT list to return the resultset you want to return. 并在SELECT列表中使用适当的表达式返回要返回的结果集。

You should be able to do this, assuming table1 and table2 are your two datatables, and resultTable is the DataTable you wish to populate with the results of the join: 您应该能够做到这一点,假设table1table2是您的两个DataTable table2 ,而resultTable是您希望用resultTable结果填充的DataTable

var res = from dr1 in table1.AsEnumerable()
            join dr2 in table2.AsEnumerable()
              on dr1.Field<int>("ID") equals dr2.Field<int>("ID")
          select resultTable.LoadDataRow(new object[]
          {
            dr1.Field<int>("ID"),
            dr1.Field<string>("column name .. "),
            ... 
            dr2.Field<string>("column from table 2 .. "),
            ...
           }, false);

and resultTable will then be populated with the rows with the fields you've chosen in the select. resultTable然后将与您在选择中选择的字段行进行填充。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM