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

时间:2015-07-20 17:09:51

标签: c# .net linq join

我尝试使用Linq基于多个条件加入两个DataTable。我能够使用这篇文章进行合并:Combining two tables into one。但是,该示例仅加入一个主键,我需要能够指定多个条件。

DataTable1(代码中的table1)

+-----------------------------------------------+
| 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)

+-------------------------------+
| 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

2 个答案:

答案 0 :(得分:4)

要加入多个列,您必须使用匿名类型:

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"),
    (...)
}

答案 1 :(得分:0)

Fiel您需要使用新的{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;