Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am going to bind a datatable by linq query to database
Below is my query..

C#
DataTable table = new DataTable();
               IEnumerable<double> query = (from st in obj.Book_details
                               select new {st.Category,st.Book_Id,st.Edition,st.Name,st.Price,st.Quantity,st.Quick_overview }).Take(20);
               query.CopyToDataTable(table, LoadOption.PreserveChanges);<pre lang="text">



now i am getting error in last line where i bind my table
and the error is that there is no boxing
Error	149	The type 'double' cannot be used as type parameter 'T' in the generic type or method 'System.Data.DataTableExtensions.CopyToDataTable<T>(System.Collections.Generic.IEnumerable<T>, System.Data.DataTable, System.Data.LoadOption)'. There is no boxing conversion from 'double' to 'System.Data.DataRow'.
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<double>'. An explicit conversion exists (are you missing a cast?)	
Posted
Updated 8-Dec-13 19:01pm
v2
Comments
Jusitn_H 9-Dec-13 1:01am    
I don't use linq often (so I could be way off) but I'm not seeing why IEnumerable<<double>> would be the appropriate type here. It looks like the query will return rows (books). Try var query = ... and see if that takes care of it.
Omprakash Kukana 9-Dec-13 1:03am    
The type 'AnonymousType#1' cannot be used as type parameter 'T' in the generic type or method 'System.Data.DataTableExtensions.CopyToDataTable<t>(System.Collections.Generic.IEnumerable<t>, System.Data.DataTable, System.Data.LoadOption)'. There is no implicit reference conversion from 'AnonymousType#1' to 'System.Data.DataRow'.

1 solution

You cannot use query.CopyToDataTable() method if it returns other than a DataRow type. Here it is Double.
Should consider adding an extension method if to take care of any other datatypes

Refer the below link which shows how to handle in case of non DataRow type

http://msdn.microsoft.com/en-us/library/bb669096.aspx[^]

Hope this helps
 
Share this answer
 
Comments
Omprakash Kukana 9-Dec-13 6:32am    
this is not helping me out
JoCodes 9-Dec-13 6:59am    
Have you understood why the error is coming?
Omprakash Kukana 10-Dec-13 2:26am    
no i didnt understand
JoCodes 11-Dec-13 0:34am    
Check the link which i have posted in the solution . .CopyToDataTable() expects a datarow type only not a double. So you have to implement it as generic way to support it.

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