The existing CopyToDataTable methods only operate on an
IEnumerable(Of
T) source where the generic parameter
T is of type DataRow. Although this is useful, it does not allow tables to be created from a sequence of scalar types, from queries that return anonymous types,
or from queries that perform table joins. For an example of how to implement two custom
CopyToDataTable methods that load a table from a sequence of scalar or anonymous types, see
How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRows.
Following that link takes you to some information that seems to apply to .net framework 4 only, and I'm working in 3.5.
Here is my linq query :
var joinedData = from dataRow2 in dataTable2.AsEnumerable()
join dataRow1 in dataTable1.AsEnumerable()
on (int)dataRow2["id"] equals (int)dataRow1["id"]
select new
{
value = (string)dataRow2["text"]
};
I have verified thta it returns one row. The reference above has an example that suggests this line should work:
However, it throws an error. Also, efforts to use IEnumerable<DataRow> instead of var as the datatype for the linq query fail as soon I introduce the join.
Is there a way to write a linq query that joins two DataTables and get the results as a DataTable using framework 3.5?
Hi, your linq query should result in IEnumerable<string> not IEnumerable<DataRow>, becouse you return srtings
but you can modify it to return something like this :-
var query = from rowx in dt.AsEnumerable()
join rowy in dt2.AsEnumerable()
on rowx["id"] equals rowy["id"]
select rowx;
DataTable dt3 = query.CopyToDataTable();
Hi, your linq query should result in IEnumerable<string> not IEnumerable<DataRow>, becouse you return srtings
but you can modify it to return something like this :-
var query = from rowx in dt.AsEnumerable()
join rowy in dt2.AsEnumerable()
on rowx["id"] equals rowy["id"]
select rowx;
DataTable dt3 = query.CopyToDataTable();
Thank you for the reply. I'm using a simple example. My long term goal is to select fields from both DataTables so I need a way to accomplish that.
My long term goal is to select fields from both DataTables so I need a way to accomplish that.
According to this situation,I think a better way is to use a normal select statemenet of SQL and with the help of SqlDataAdapter's Fill method instead of using LINQ。
var query = from c in dt.AsEnumerable()
join o in dt1.AsEnumerable() on c.Field<Int32>("Row_Index") equals o.Field<Int32>("Row_Index")
select new
{
Row_Index = c.Field<Int32>("Row_Index"),
Revision = o.Field<Int32>("Revision"),
Column_ID = c.Field<Int32>("Column_ID"),
Module_ID = c.Field<Int32>("Module_ID"),
Stack_ID = c.Field<String>("prodid"),
newStack_ID = o.Field<String>("prodid")
};
I need to get the above query result into new datatable................
Dan Bracuk
Contributor
3970 Points
1096 Posts
Linq to DataTables Result as DataTable
Dec 20, 2011 02:24 PM|LINK
This page, http://msdn.microsoft.com/en-us/library/bb386921.aspx, mentions this:
The existing CopyToDataTable methods only operate on an IEnumerable(Of T) source where the generic parameter T is of type DataRow. Although this is useful, it does not allow tables to be created from a sequence of scalar types, from queries that return anonymous types, or from queries that perform table joins. For an example of how to implement two custom CopyToDataTable methods that load a table from a sequence of scalar or anonymous types, see How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRows.
Following that link takes you to some information that seems to apply to .net framework 4 only, and I'm working in 3.5.
Here is my linq query :
var joinedData = from dataRow2 in dataTable2.AsEnumerable()
join dataRow1 in dataTable1.AsEnumerable()
on (int)dataRow2["id"] equals (int)dataRow1["id"]
select new
{
value = (string)dataRow2["text"]
};
I have verified thta it returns one row. The reference above has an example that suggests this line should work:
DataTable dataTable = joinedData.CopyToDataTable();
However, it throws an error. Also, efforts to use IEnumerable<DataRow> instead of var as the datatype for the linq query fail as soon I introduce the join.
Is there a way to write a linq query that joins two DataTables and get the results as a DataTable using framework 3.5?
Ahmed Moosa
Star
7784 Points
1232 Posts
Re: Linq to DataTables Result as DataTable
Dec 20, 2011 03:19 PM|LINK
Hi, your linq query should result in IEnumerable<string> not IEnumerable<DataRow>, becouse you return srtings
but you can modify it to return something like this :-
var query = from rowx in dt.AsEnumerable() join rowy in dt2.AsEnumerable() on rowx["id"] equals rowy["id"] select rowx; DataTable dt3 = query.CopyToDataTable();MCC - MCPD -MCTS
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Linq to DataTables Result as DataTable
Dec 20, 2011 03:30 PM|LINK
Thank you for the reply. I'm using a simple example. My long term goal is to select fields from both DataTables so I need a way to accomplish that.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Linq to DataTables Result as DataTable
Dec 22, 2011 01:34 AM|LINK
According to this situation,I think a better way is to use a normal select statemenet of SQL and with the help of SqlDataAdapter's Fill method instead of using LINQ。
neerajkumarm...
Member
35 Points
35 Posts
Re: Linq to DataTables Result as DataTable
Feb 01, 2013 09:21 AM|LINK
var query = from c in dt.AsEnumerable()
join o in dt1.AsEnumerable() on c.Field<Int32>("Row_Index") equals o.Field<Int32>("Row_Index")
select new
{
Row_Index = c.Field<Int32>("Row_Index"),
Revision = o.Field<Int32>("Revision"),
Column_ID = c.Field<Int32>("Column_ID"),
Module_ID = c.Field<Int32>("Module_ID"),
Stack_ID = c.Field<String>("prodid"),
newStack_ID = o.Field<String>("prodid")
};
I need to get the above query result into new datatable................
Thanks in Advance
Engg Software
Mumbai