Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

SQL
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:

LINQ
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?
Posted
Updated 9-Nov-13 11:42am
v2

1 solution

Creating a data table at all makes no sense. Accessing fields the way you are, makes no sense. Do you not have a data model so you can just do tbl2.Id ? Why use EF then stuff the result in to a data table ?

You can probably use reflection to do this. But why would you ? If you're creating a join commonly, you should define a class to contain the results, and select a list of those. I'd probably go one step further and do the request in a procedure, and have it return a strongly typed class, and be done with it.
 
Share this answer
 
Comments
MrGlass3 9-Nov-13 17:45pm    
Code you provide some example code doing what you are describing? Sorry I am new to LINQ. I would still be interested in finding the solution to my original question of creating a new DataTable containing certiain Fields from two different DataTables, if that is possible. My main reason for wanting a DataTable with this data is so I can easily assign the Source of a DataGridView equal to this new DataTable.
Christian Graus 9-Nov-13 18:00pm    
Your datagridview will take any IListSource, AFAIK. http://social.msdn.microsoft.com/Forums/en-US/5ad19756-376f-47a3-9a5d-2cb4837ce44a/bind-datagridview-datasource-with-ienumerable?forum=linqtosql is a post on this.

If you create a new EDMX in your project, and build it from your DB, you will get a collection of classes that represent the tables, there will be a Table1 class and a Table2 class. You'd need to write your own class to contain the result of more than one table joined together, but this can also be done, by writing mapping classes, or by using the Database object to call a proc, it's generic and you specify the name of the class that has members to map to the return values.
MrGlass3 9-Nov-13 19:10pm    
I guess I'll have to spend some time looking into what you have said, off the bat I wasn't really able to follow. Is what I was originally asking about - creating a combined DataTable not possible?
Christian Graus 9-Nov-13 19:25pm    
You could perhaps use reflection, although I'm not sure it works well with var, it should. But it's just a horrible way to do things, you're basically ignoring the benefits of EF, and may as well not be using it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900