Click here to Skip to main content
14,447,769 members
Rate this:
Please Sign up or sign in to vote.
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:
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 17:49pm
v2
Comments
Tomas Takac 27-Jan-15 3:15am
   
You are selecting a decimal, not a DataRow.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

you need to select data row in your linq query, for example
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>
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100