Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a linq to dataset query that joins two tables and extracts the desired parameters from each. I need to get them into a DataTable to bind to a DataGridView. The example I found for doing this on MSDN is a trivial example taking a single value from a single table, but when I tried to change my query to follow it I was unable to do so. The CopyToDataTable() method requires that the query be assigned to a IEnumerable<datarow>, but when I do so I'm told that an explict cast is needed; but the cast fails at runtime with the exception:

Unable to cast object of type '<JoinIterator>d__61`4[System.Data.DataRow,System.Data.DataRow,System.Int32,<>f__AnonymousType0`1[System.Int32]]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.

Original working query:
var query = MyDataSet.Table1.AsEnumerable().Join(MyDataSet.Table2.AsEnumerable(),
        table1 => table1.Field<Int32>("Table1_Id"),
        table2 => table2.Field<Int32>("Table1_Id"),
        (table1, table2) => new
        {
                Table1ID = table1.Field<Int32>("Table1_Id")
                //Other parameters commented out to simplify the example
        });


Non-working query with explicit cast:

IEnumerable<DataRow> query = (IEnumerable<DataRow>)MyDataSet.Table1.AsEnumerable().Join(MyDataSet.Table2.AsEnumerable(),
        table1 => table1.Field<Int32>("Table1_Id"),
        table2 => table2.Field<Int32>("Table1_Id"),
        (table1, table2) => new
        {
                Table1ID = table1.Field<Int32>("Table1_Id")
                //Other parameters commented out to simplify the example
        });
Posted

1 solution

Perhaps the answer to a similar question will be of use to you.

"when I do so I'm told that an explict cast is needed" -- what's the error message you get?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900