colinwurtz
colinwurtz

Reputation: 723

Outer Joining two Data Tables on multiple criteria

I'm trying to join two DataTables using Linq based on multiple criteria. I was able to get a merge working using this post: Combining two tables into one. However, that example only joins on one primary key and I need to be able to specify multiple criteria.

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

I want the output to look like this:

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

Here is what I have so far:

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

Upvotes: 1

Views: 1154

Answers (2)

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

FielYou need to Join using Anonymous Type by using 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; 

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

To join on multiple columns you have to use anonymous types:

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

Upvotes: 4

Related Questions