Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to COnvert Linq to a Data Table using the below code I'm getting and exception. Please help me to solve this. The Details are below:

Code:
C#
DataTable boundTable = new DataTable();

IEnumerable<DataRow> results1 = (IEnumerable<DataRow>) (
  from rowDict in dsEstimatesParam.Tables[strTableName].AsEnumerable()
  join rowMaster in dsAttributes.Tables[strTableName1].AsEnumerable()
    on rowDict["ID"] equals rowMaster["ID"]
  join rowAttDep in dsParam.Tables["Dependencies"].AsEnumerable()
    on rowMaster["ID"] equals rowAttDep["ID"]
  where Convert.ToString(rowAttDep["Name"]) == strColumn
  select rowDict[strColumn] == DBNull.Value ? 0 : Convert.ToDecimal(rowDict[strColumn]));

boundTable = results1.CopyToDataTable<DataRow>();

Exception:
Unable to cast object of type 'WhereSelectEnumerableIterator`2[<>f__AnonymousType5`2[<>f__AnonymousType1`2[System.Data.DataRow,System.Data.DataRow],System.Data.DataRow],System.Decimal]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'

Thanks & Regards,
Mathi.
Posted
Updated 26-Nov-19 16:49pm
v2
Comments
Tomas Takac 27-Jan-15 3:15am    
You are selecting a decimal, not a DataRow.

1 solution

you need to select data row in your linq query, for example
C#
IEnumerable<datarow> query =
  from rowDict in dsEstimatesParam.Tables[strTableName].AsEnumerable()
  join rowMaster in dsAttributes.Tables[strTableName1].AsEnumerable()
    on rowDict["ID"] equals rowMaster["ID"]
  join rowAttDep in dsParam.Tables["Dependencies"].AsEnumerable()
    on rowMaster["ID"] equals rowAttDep["ID"]
  where Convert.ToString(rowAttDep["Name"]) == strColumn
  select rowDict;
DataTable boundTable = query.CopyToDataTable<datarow>();</datarow></datarow>
 
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