Click here to Skip to main content
Click here to Skip to main content

Convert Linq Query Result To DataTable

By , 5 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Sometimes you need the Linq query result as datatable (I need it today)
Use this:
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query)
            if (query == null)
                throw new ArgumentNullException("query");
            IDbCommand cmd = ctx.GetCommand((IQueryable)query);
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
            adapter.SelectCommand = (System.Data.SqlClient.SqlCommand)cmd;
            DataTable dt = new DataTable("dataTbl");
                adapter.FillSchema(dt, SchemaType.Source);
            return dt;
P.S.: I'm not the writer of this code, I just Googled it some time ago.


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

About the Author

Rami Shareef
Software Developer KATAKIT
Syrian Arab Republic Syrian Arab Republic
I never done anything before

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 5 Apr 2010
Article Copyright 2010 by Rami Shareef
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid