根据多个标准外部连接两个数据表

本文关键字:两个 数据表 外部 标准 连接 | 更新日期: 2023-09-27 18:03:42

我试图根据多个标准使用Linq加入两个数据表。我能够得到一个合并工作使用这篇文章:将两个表合并成一个。但是,该示例只在一个主键上连接,并且我需要能够指定多个条件。

DataTable1(table1 in code)

+-----------------------------------------------+
| CLIENT,USER_NAME,LAST_LOGIN_DT_TM,BIRTH_DT_TM |
+-----------------------------------------------+
| CLIENT1, USER1, 30-2014, 5-5-1980             |
| CLIENT1, USER2, 12-21-2014, 5-2-1990          |
| CLIENT2, USER3, 11-30-2014, 5-1-1950          |
| CLIENT2, USER4, 12-15-2014, 1-1-1900          |
+-----------------------------------------------+

DataTable 2(table2 in code)

+-------------------------------+
| CLIENT,USER_NAME,TOTAL_ORDERS |
+-------------------------------+
| CLIENT1, USER1, 1500          |
| CLIENT2, USER2, 2500          |
+-------------------------------+

我希望输出看起来像这样:

+-------------------------------------------------------------+
| CLIENT,USER_NAME,LAST_LOGIN_DT_TM,BIRTH_DT_TM, TOTAL_ORDERS |
+-------------------------------------------------------------+
| CLIENT1, USER1, 1-30-2014, 5-5-1980, 1500                   |
| CLIENT1, USER2, 12-21-2014, 5-2-1990,                       |
| CLIENT2, USER2, 11-30-2014, 5-1-1950, 2500                  |
| CLIENT2, USER1, 12-15-2014, 1-1-1900,                       |
+-------------------------------------------------------------+

到目前为止我写的是:

DataTable targetTable = table1.Clone();
var dt2Columns = table2.Columns
                       .OfType<DataColumn>()
                       .Select(dc => new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
var dt2FinalColumns = from dc in dt2Columns.AsEnumerable()
                      where targetTable.Columns.Contains(dc.ColumnName) == false
                      select dc;
                      targetTable.Columns.AddRange(dt2FinalColumns.ToArray());
//  This only matches on USER_NAME now, how do I add in CLIENT?
var rowData = from row1 in table1.AsEnumerable()
              join row2 in table2.AsEnumerable()
              on row1.Field<string>("USER_NAME") equals row2.Field<string>("USER_NAME")
              select row1; 
// This gives me the matched rows in rowData, but I'm not sure how to join it with original table1

根据多个标准外部连接两个数据表

要连接多个列,必须使用匿名类型:

from row1 in table1.AsEnumerable()
join row2 in table2.AsEnumerable() on
  new { userName = row1.Field<string>("USER_NAME"), client = row1.Field<string>("CLIENT") }
equals
  new { userName = row2.Field<string>("USER_NAME"), client = row2.Field<string>("CLIENT") }
select new {
    userName = row1.Field<string>("USER_NAME"),
    client = row1.Field<string>("CLIENT"),
    totalOrders = row2.Field<int>("TOTAL_ORDERS"),
    (...)
}

field您需要使用new {Field1, Field2}使用匿名类型加入

IEnumerable<DataRow> rowData = from row1 in table1.AsEnumerable()
          join row2 in table2.AsEnumerable()
          on new { col1 = row1.Field<string>("USER_NAME"),  col2 = row1.Field<string>("SecondField") }  equals {col1 = row2.Field<string>("USER_NAME") , col2 = row2.Field<string>("SecondField") }
          select row1;