Q:

How to convert Linq result to Datatable (using datatable not context)

How can i convert linq result to data table?
Dim query =  (From e In dsCart.Tables(0).AsEnumerable()
          Group e By
            DistNum = e("DistNum"),
            DistributorName = e("DistributorName"),
            SourceID = e("SourceID")
          Into Group
          Select New With {
            .DistNum = DistNum,
            .DistributorName = DistributorName,
            .TotalOrderAmt = Group.Sum(Function(x) x.Field(Of Decimal)("ExtendedAmt")),
            .ItemQty = Group.Count(Function(x) x.Field(Of Integer)("ItemQty"))
         })

I have come across the following method to convert linq var to data table but i am getting error 'The type arguments for method cannot be inferred from the usage' when calling gv.DataSource = ToDataTable(query)
private Function ToDataTable(Of T)(collection As IEnumerable(Of T)) As DataTable
    Dim dt As New DataTable()
    Dim _t As Type = GetType(T)
    Dim pia As PropertyInfo() = _t.GetProperties()

    'Create the columns in the DataTable
    For Each pi As PropertyInfo In pia
        dt.Columns.Add(pi.Name, If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType))
    Next

    'Populate the table
    For Each item As T In collection
        Dim dr As DataRow = dt.NewRow()
        dr.BeginEdit()
        For Each pi As PropertyInfo In pia
            dr(pi.Name) = pi.GetValue(item, Nothing)
        Next
        dr.EndEdit()
        dt.Rows.Add(dr)
    Next

    Return dt
End Function

A:

I wrote a method similar to the one you found but it has a couple of improvements.  First, it's an extension method so it can be called like an instance method.  That's a small thing but, for many, that feels more natural.  Secondly, and more importantly, it handles NULLs.
Imports System.Reflection
Imports System.Runtime.CompilerServices

''' <summary>
''' Contains methods that extend the <see cref="IEnumerable(Of T)"/> interface.
''' </summary>
Public Module EnumerableExtensions

    ''' <summary>
    ''' Returns a <see cref="DataTable"/> containing the data from the source list.
    ''' </summary>
    ''' <typeparam name="T">
    ''' The type of the list items.
    ''' </typeparam>
    ''' <param name="source">
    ''' The source list.
    ''' </param>
    ''' <returns>
    ''' A <see cref="DataTable"/> with a column for each public property in the item type and a row for each item.
    ''' </returns>
    <Extension>
    Public Function ToDataTable(Of T)(source As IEnumerable(Of T)) As DataTable
        Dim sourceType = GetType(T)
        Dim properties = sourceType.GetProperties()
        Dim table As New DataTable

        'Add a column for each public instance property of the items.
        For Each prop In properties
            table.Columns.Add(prop.Name,
                              If(Nullable.GetUnderlyingType(prop.PropertyType),
                                 prop.PropertyType))
        Next

        'Add a row for each item.
        For Each item In source
            Dim row = table.NewRow()

            row.BeginEdit()

            For Each prop In properties
                row(prop.Name) = If(prop.GetValue(item), DBNull.Value)
            Next

            row.EndEdit()

            table.Rows.Add(row)
        Next

        Return table
    End Function

End Module

