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
ColumnB = TableA.Field
}
equals new
{
ColumnA = TableB.Field
ColumnB = TableB.Field
}
into GJ
from sub in GJ.DefaultIfEmpty()
select new
{
Column1 = TableA.Field
Column2 = TableA.Field
Column3 = sub == null ? TableA.Field
sub.Table.TableName.ToUpper().Contains("Operation to perform") ? Convert.ToString
(Convert.ToDecimal(TableA.Field
Convert.ToDecimal(sub.Field
};
dtJoinedTable = resultQuery.ExtCopyToDataTable();
return dtJoinedTable;
}
6 comments:
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>};
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>};
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();
Post a Comment