Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dt = db.CustomerMasters.OrderBy(x => x.CustName); 

is not supporting how to convert it to data table???
Posted
Updated 12-Jun-12 2:44am
v2

Hi you can try the below solution.

C#
IEnumerable<DataRow> query = db.CustomerMasters.OrderBy(x => x.CustName) 
//Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();


or check this link

http://stackoverflow.com/questions/4460654/best-practice-convert-linq-query-result-to-a-datatable-without-looping[^]

//Edits to merge two answers into one and to clean up code paste issues - TimCorey
 
Share this answer
 
v3
Comments
Tim Corey 12-Jun-12 9:02am    
I updated this question to have both of your answers. I also cleaned up your code to be more readable. I hope I got it all right (you might want to check it). In the future, please use the "Improve Solution" link to update your answer.
Prasad_Kulkarni 12-Jun-12 9:04am    
Good work Tim!
Manas Bhardwaj 12-Jun-12 9:15am    
5ed!
VJ Reddy 12-Jun-12 11:01am    
Good answer. 5!
Please refer:
http://msdn.microsoft.com/en-us/library/bb386921.aspx[^]
Creating a DataTable From a Query (LINQ to DataSet)

http://msdn.microsoft.com/en-us/library/bb387004.aspx[^]
This link provides conceptual information and examples for programming with LINQ to DataSet.

http://msdn.microsoft.com/en-us/library/bb669096.aspx[^]
This topic describes how to implement two custom CopyToDataTable<t> extension methods that accept a generic parameter T of a type other than DataRow.
 
Share this answer
 
Comments
Manas Bhardwaj 12-Jun-12 9:15am    
5ed!
Prasad_Kulkarni 12-Jun-12 23:42pm    
Thank you Manas!
VJ Reddy 12-Jun-12 11:01am    
Good references. 5!
Prasad_Kulkarni 12-Jun-12 23:42pm    
Thank you VJ!

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