in

vbCity Blogs

New (temp) place for vbCity Blogs

Mike McIntyre's .NET Journal

Visual Basic 2008 - Create a DataTable From Linq Query Results

Below is a class that contains a method that can create a DataTable object form Linq Query Results. I find the LINQToDataTable method really handy when I am creating Crystal reports. Crystal Reports love to report on DataTables.

The Imports are essential to the LINQToDataTable method in the LinqUtilities class.

Imports System.Data
Imports System.Linq
Imports System.Reflection
 
Public Class LinqUtilities
    Friend Shared Function LINQToDataTable(Of T)(ByVal iEnumerableList As IEnumerable(Of T)) As DataTable
        Dim newDataTable As New DataTable()
        Dim thePropertyInfo As PropertyInfo() = Nothing
        If iEnumerableList Is Nothing Then
            Return newDataTable
        End If
 
        For Each item As T In iEnumerableList
            If thePropertyInfo Is Nothing Then
                thePropertyInfo = (DirectCast(item.[GetType](), Type)).GetProperties()
                For Each propInfo As PropertyInfo In thePropertyInfo
                    Dim columnDataType As Type = propInfo.PropertyType
                    If (columnDataType.IsGenericType) AndAlso (columnDataType.GetGenericTypeDefinition() Is GetType(Nullable(Of ))) Then
                        columnDataType = columnDataType.GetGenericArguments()(0)
                    End If
                    newDataTable.Columns.Add(New DataColumn(propInfo.Name, columnDataType))
                Next
            End If
 
            Dim dr As DataRow = newDataTable.NewRow()
            For Each pi As PropertyInfo In thePropertyInfo
                dr(pi.Name) = If(pi.GetValue(item, Nothing) Is Nothing, DBNull.Value, pi.GetValue(item, Nothing))
            Next
 
            newDataTable.Rows.Add(dr)
        Next
        Return newDataTable
    End Function
 
End Class

Below is example code that uses Linq to query for all the buttons on a Windows Forms form and calls the LINQToDataTable method to put the query results into a DataTable.

Dim newDataTable As DataTable = _
        LinqUtilities.LINQToDataTable(From btn In Me.Controls _
            Where TypeOf (btn) Is Button Select btn)

Mike McIntyre's .Net Journal

getdotnetcode.com

Only published comments... Nov 06 2009, 07:40 AM by Mike McIntyre
Filed under: ,

About Mike McIntyre

I  am a programmer, developer, and system architect with 29+years experience including state-of-the-art application development frameworks and languages including Visual Studio versions 2002-2010, .NET Framework versions 1-4, C#, and Visual Basic.

I am a mentor, trainer, and coach in the Microsoft .NET technical community. DevCity is my favorite community site. You can find examples of my involvement with DevCity in the site articles, newsletter articles, and my posts in the forum.

Since 2002 I have hosted a .NET site at http://www.getdotnetcode.com, a place with free and inexpensive Visual Basic and C# source code that can be purchased on an 'as needed' basis.

In 2007 I established a .NET search engine, 'Resources for .NET Developers' at http://dot-net-resources-swicki.eurekster.com

Copyright 1998-2009 vbCity.com LLC
Powered by Community Server (Non-Commercial Edition), by Telligent Systems