Answered by:
Linq results into a datatable

Question
-
Is it possible to convert a Linq result to a datatable?
I've seen numerous re fences to a method called "CopyToDataTable()" but it doesn't show up in my intellisence, do I have to import something different to gain access to this?
For the time being I've manually made a datatable and iterated through each Linq result and placed it in the datatable which can't be very efficient, is there a better way?
Thanks
Ed
Friday, November 23, 2007 11:26 AM
Answers
-
Ed,
While there are some good examples of how to do this out there, there is nothing built in. See the following link for a way to do this in your own code.
http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx
Thanks,
Erick
Wednesday, November 28, 2007 10:02 PM
All replies
-
Ed,
If your LINQ query selects a DataRow, you can use CopyToDataTable to move the results into another DataTable. If you are doing any sort of projection, you'll need to manually copy over the results. I'm going to have a blog post in the next few weeks that shows how to do this in detail.
Some information to get you started:
http://blogs.msdn.com/erickt/archive/2007/08/24/linq-to-dataset-data-binding.aspx
Thanks,
Erick
http://blogs.msdn.com/erickt/archive/2007/08/24/linq-to-dataset-data-binding.aspx
Tuesday, November 27, 2007 12:51 AM -
Thanks Erick,
What I really wanted to use Linq for SQL to interogate a database and then for convenience copy the Linq results into a datatable. I've done a simple for each statement to do this manually but thought there may have been a more optimal way built in.
Ed
Tuesday, November 27, 2007 8:36 AM -
Ed,
While there are some good examples of how to do this out there, there is nothing built in. See the following link for a way to do this in your own code.
http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx
Thanks,
Erick
Wednesday, November 28, 2007 10:02 PM -
Thanks for the advice Erick, I think I'll just have to loop through the items to build a datatable manually if I need to do this.
Ed
Thursday, November 29, 2007 8:46 AM