Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Convert LINQ to Entity Result to a DataTable

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
22 Mar 2011CPOL 58.1K   12   7

Introduction

This article helps converting LINQ to Entity result to a data table. This can be very useful if you are moving to entity framework and still want to support existing methods, write a normal Linq to entity syntax, then with a simple extension method convert it to a data table. Note that the Query is executed once on the server and there is no need to loop the result and create the data table which takes too much time.


Using the code

These two extension methods help in getting the data table directly form the result.


VB
Imports System.Data.Objects 
Imports System.Linq.Expressions
Imports System.Data.SqlClient

//Public Module EntityToDataTable

//<extension()> _
Public Function EntityToDatatable(ByVal Result As IQueryable, ByVal Ctx As  ObjectContext) As DataTable
    Try
        Using SQLCon As New SqlConnection(CType(Ctx.Connection, EntityConnection).StoreConnection.ConnectionString)
            Using Cmd As New SqlCommand(CType(Result, ObjectQuery).ToTraceString, SQLCon)
                For Each Param As ObjectParameter In CType(Result, ObjectQuery).Parameters
                    Cmd.Parameters.AddWithValue(Param.Name, Param.Value)
                Next
                Using DA As New SqlDataAdapter(Cmd)
                    Using DT As New DataTable
                        DA.Fill(DT)
                        Return DT
                    End Using
                End Using
            End Using
        End Using
    Catch
        Throw
    End Try
End Function

//<extension()> _
Public Sub EntityToDatatable(ByVal Result As IQueryable, ByVal Ctx As ObjectContext, ByVal DT As DataTable)
     Try
         Using SQLCon As New SqlConnection(CType(Ctx.Connection, EntityConnection).StoreConnection.ConnectionString)
             Using Cmd As New SqlCommand(CType(Result, ObjectQuery).ToTraceString, SQLCon)
                 For Each Param As ObjectParameter In CType(Result, ObjectQuery).Parameters
                     Cmd.Parameters.AddWithValue(Param.Name, Param.Value)
                 Next
                 Using DA As New SqlDataAdapter(Cmd)
                     DA.Fill(DT)
                 End Using
             End Using
         End Using
     Catch
         Throw
     End Try
 End Sub

  //End Module          

Hope it helps.

Development to me is a pleasure more than a job.

License

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


Written By
Software Developer CME Offshore
Lebanon Lebanon
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dean Henderson3-Jul-12 11:17
Dean Henderson3-Jul-12 11:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.