Thursday, November 17, 2011

Left Outer Join of Two DataTable Using Linq


This method will take two table as input. This function will provide Left Outer join between two table and also performs any kind of math operation needed on the table. For example : along with the left outer join we may want to add columns of two table to produce different column value. In below example two columns from two different table are added . After the Left Outer join, Left Table will contain added value .

public static DataTable LeftOuterJoinTables(DataTable LeftTable, DataTable RightTable)
        {
            DataTable dtJoinedTable = null;
            var resultQuery = from TableA in LeftTable.AsEnumerable()
                                join TableB in RightTable.AsEnumerable()
                                on new
                                {
                                    ColumnA = TableA.Field(LeftTable.Columns[0]),
                                    ColumnB = TableA.Field(LeftTable.Columns[1])
                                }
                                equals new
                                {
                                    ColumnA = TableB.Field(RightTable.Columns[0]),
                                    ColumnB = TableB.Field(RightTable.Columns[1])
                                }
                                into GJ
                                from sub in GJ.DefaultIfEmpty()
                                select new
                                {
                                    Column1 = TableA.Field(LeftTable.Columns[0]),
                                    Column2 = TableA.Field(LeftTable.Columns[1]),
                                    Column3 = sub == null ? TableA.Field(LeftTable.Columns["Variable Column"]) :
                                              sub.Table.TableName.ToUpper().Contains("Operation to perform") ? Convert.ToString
                                              (Convert.ToDecimal(TableA.Field(LeftTable.Columns["Column Conataining Values"])) +
                                              Convert.ToDecimal(sub.Field(RightTable.Columns["Column Conataining Values"]))) : ""
                                };
                       dtJoinedTable = resultQuery.ExtCopyToDataTable();


            return dtJoinedTable;
        }

6 comments:

RajkumarMasilamani said...

Hi,
I got error in select new ..am not able to select particular Field.

var res_new = from resdt in stud.AsEnumerable()
join resdt1 in stud1.AsEnumerable()
on resdt.Field(0) equals resdt1.Field(2)
select new { Column1 = resdt.Field<0>};

RajkumarMasilamani said...
This comment has been removed by the author.
RajkumarMasilamani said...
This comment has been removed by the author.
RajkumarMasilamani said...
This comment has been removed by the author.
RajkumarMasilamani said...

I am not able to select result column.
var res_new = from resdt in stud.AsEnumerable()
join resdt1 in stud1.AsEnumerable()
on resdt.Field(0) equals resdt1.Field(2)
select new { Column1 = resdt.Field<0>};

alan baum said...

I was working in sharepoint and needed to join two lists. I couldn't get spquery to join properly since in my situation because I'm not using lookup fields in the left list. I couldn't get this to work but you got me going in the right direction. Here is the best example of a left join I've found: http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx.

Also for anyone working in sharepoint with a similar situation I think best practice is to use a spquery to get your data from your lists. Convert them into datatables and then use linq.
http://www.pranavsharma.com/blog/2011/12/16/large-list-performance-spmetal-vs-spquery/

SPList list1 = objweb.Lists["list1"];
SPList list2 = objweb.Lists["list2"];
SPListItemCollection itemsCollection = list1.GetItems(query1);
SPListItemCollection itemsCollection2 = list2.GetItems(query2);
DataTable list1Data = itemsCollection.GetDataTable();
DataTable list2Data = itemsCollection2.GetDataTable();