I have two DataTables with one field connecting the two tables. I want to join the two tables and get some (or all) of the columns from each DataTable into a new one. I wouldn't think this would be difficult using LINQ but I am at a loss.
In SQL it is easy, I would just write
SELECT Table1.ID, Table1.Field1, Table2.Field2
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table12.ID
What is the LINQ equivalent to the SQL code above??
All the examples I find look something like this:
var query =
from tbl1 in Table1.AsEnumerable()
join tbl2 in Table2.AsEnumerable() on tbl1["ID"] equals tbl2["ID"]
select new { ID = tbl1["ID"], Field1 = tbl1["Field1"], Field2 = tbl2"Field2"]};
DataTable newTable = new DataTable();
newTable.Columns.Add("ID",typeof(Guid));
newTable.Columns.Add("Field1", typeof(string));
newTable.Columns.Add("Field2",typeof(string));
foreach (var rowInfo in query)
{
newTable.Rows.Add(rowInfo.ID, rowInfo.Field1, rowInfo.Field2);
}
That is a fine solution if they are really small tables but I'm working with tables that have 10 or so columns and that would look really messy. Is there not a more straight-forward way like in SQL?