Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, September 6, 2008 12:51 AM
I have 2 datatables like below and I want to create the third table using LINQ.
The key of both is ID. I want to join the two datatables so that I have all the records of the first table (left join).
Can someone help me figure this out.
FirstDataTable - Table 1
ID Type Col1 Col2
2 Blue XYZ ABC
4 Green LMN CDF
1 Orange OPE BGD
Second Table: Table2
ID Part Quantity
1 425687 10
2 425659 25
3 395678 30
4 426589 50
5 423568 25
Desired result (in a dataTable if it's possible)
ID Type Part Quantity
1 Orange 425687 10
2 Blue 425659 25
4 Green 426589 50
All replies (2)
Monday, September 8, 2008 11:00 PM âś…Answered
Hi sandipchandra17,
First of all, it's possible to use LINQ query with DataTable objects. Since you want to do a left join, you could try this:
var ret = from p in Table1.AsEnumerable() join q in Table2.AsEnumerable() on p.Field<int>("ID") equals q.Field<int>("ID") into UP from q in UP.DefaultIfEmpty() select new { ID = p.Field<int>("ID"), Type = p.Field<string>("Type"), Part = q.Field<int>("Part"), Quantity = q.Field<int>("Quantity") };
Note: you have to specify the correct type of each field.
Thanks.
Sunday, September 7, 2008 3:38 AM
Here is an article that you might read up on if you need to use LINQ. http://www.code-magazin.de/article.aspx?quickid=0712052&page=2
Are you getting your data from a database? If so just why don't you do the join in SQL and just return one datatable??