object combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable()
join dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
select new { dt1, dt2 };
DataTable finaldt = new DataTable("FinalTable");
finaldt.Columns.Add(new DataColumn("sp",typeof(string)));
finaldt.Columns.Add(new DataColumn("Method",typeof(string)));
finaldt.Columns.Add(new DataColumn("Class",typeof(string)));
finaldt.Columns.Add开发者_运维技巧(new DataColumn("BLLMethod",typeof(string)));
DataRow newrow = finaldt.NewRow();
finaldt.Rows.Add((DataRow)combinedrows);
dataGridView5.DataSource = finaldt;
The above coding gives the result in the first column as follows:
System.Linq.Enumerable+d__614[System.Data.DataRow,System.Data.DataRow,System.String,<>f__AnonymousType0
2[System.Data.DataRow,System.Data.DataRow]]
@Prem: After understanding your code i am sure you will get exception like
"Unable to cast object of type 'd__614[System.Data.DataRow,System.Data.DataRow,System.String,<>f__AnonymousType0
2[System.Data.DataRow,System.Data.DataRow]]' to type 'System.Data.DataRow'."
on
finaldt.Rows.Add((DataRow)combinedrows); line
so you must store Linq return result in var and then you can add row to new DataTable by loop.
your code should be
var combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable()
join dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
select new { dt1, dt2 };
DataTable finaldt = new DataTable("FinalTable");
finaldt.Columns.Add(new DataColumn("sp", typeof(string)));
finaldt.Columns.Add(new DataColumn("Method", typeof(string)));
finaldt.Columns.Add(new DataColumn("Class", typeof(string)));
finaldt.Columns.Add(new DataColumn("BLLMethod", typeof(string)));
DataRow newrow = finaldt.NewRow();
foreach (var row in combinedrows)
{
DataRow dataRow = finaldt.NewRow();
dataRow.ItemArray = row.dt1.ItemArray;
finaldt.Rows.Add(dataRow);
}
try it out on the behalf of you i have checked it is running if not then post the error.
For to get only a particular column from DataTable you need to change LINQ like
var combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable()
join dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
select new { td1Col = dt1.Field<string>("Tab1col2")};
and for Retrieving data you need to do:
foreach (var row in combinedrows)
{
string value = row.td1Col.ToString();
}
try this
var combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable()
join dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
select new { dt1, dt2 };
DataTable finaldt = new DataTable("FinalTable");
finaldt.Columns.Add(new DataColumn("sp",typeof(string)));
finaldt.Columns.Add(new DataColumn("Method",typeof(string)));
finaldt.Columns.Add(new DataColumn("Class",typeof(string)));
finaldt.Columns.Add(new DataColumn("BLLMethod",typeof(string)));
foreach (var row in combinedrows)
{
DataRow drow = finaldt.NewRow();
drow.ItemArray = row.dt1.ItemArray;
finaldt.Rows.Add(drow34);
}
dataGridView5.DataSource = finaldt;
精彩评论