如何将Linq结果转换为数据表(使用数据表而不是上下文) [英] How to convert Linq result to Datatable (using datatable not context)

查看:66
本文介绍了如何将Linq结果转换为数据表(使用数据表而不是上下文)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将linq结果转换为数据表?

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"))
         })

我遇到以下方法将linq var转换为数据表,但是在调用gv.DataSource = ToDataTable(query)时出现错误无法从用法中推断出方法的类型参数"

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

推荐答案

我编写了一种方法,该方法与您发现的方法类似,但是有一些改进.首先,它是一个扩展方法,因此可以像实例方法一样调用它.这是一件小事,但对于许多人来说,这感觉更自然.其次,更重要的是,它处理NULL.

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

这篇关于如何将Linq结果转换为数据表(使用数据表而不是上下文)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆