如何将Linq结果转换为数据表(使用数据表而不是上下文)

时间:2018-09-05 23:39:14

标签: vb.net linq

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

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)时出现错误“无法从用法中推断出方法的类型参数”

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

此扩展最初来自Microsoft,并进行了少许修改以处理null(我希望C#对您来说不是问题):

public static class CustomLINQtoDataSetMethods
{
    public static DataTable CopyToDataTable<T>(this IEnumerable<T> source)
    {
        return new ObjectShredder<T>().Shred(source, (DataTable)null, (LoadOption?)null);
    }

    public static DataTable CopyToDataTable<T>(this IEnumerable<T> source, DataTable table, LoadOption? options)
    {
        return new ObjectShredder<T>().Shred(source, table, options);
    }
}

public class ObjectShredder<T>
{
    private FieldInfo[] _fi;

    private PropertyInfo[] _pi;

    private Dictionary<string, int> _ordinalMap;

    private Type _type;

    public ObjectShredder()
    {
        _type = typeof(T);
        _fi = _type.GetFields();
        _pi = _type.GetProperties();
        _ordinalMap = new Dictionary<string, int>();
    }

    public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption? options)
    {
        if (typeof(T).IsPrimitive)
        {
            return ShredPrimitive(source, table, options);
        }
        if (table == null)
        {
            table = new DataTable(typeof(T).Name);
        }
        table = ExtendTable(table, typeof(T));
        table.BeginLoadData();
        using (IEnumerator<T> e = source.GetEnumerator())
        {
            while (e.MoveNext())
            {
                if (options.HasValue)
                {
                    table.LoadDataRow(ShredObject(table, e.Current), options.Value);
                }
                else
                {
                    table.LoadDataRow(ShredObject(table, e.Current), true);
                }
            }
        }
        table.EndLoadData();
        return table;
    }

    public DataTable ShredPrimitive(IEnumerable<T> source, DataTable table, LoadOption? options)
    {
        if (table == null)
        {
            table = new DataTable(typeof(T).Name);
        }
        if (!table.Columns.Contains("Value"))
        {
            table.Columns.Add("Value", typeof(T));
        }
        table.BeginLoadData();
        using (IEnumerator<T> e = source.GetEnumerator())
        {
            object[] values = new object[table.Columns.Count];
            while (e.MoveNext())
            {
                values[table.Columns["Value"].Ordinal] = e.Current;
                if (options.HasValue)
                {
                    table.LoadDataRow(values, options.Value);
                }
                else
                {
                    table.LoadDataRow(values, true);
                }
            }
        }
        table.EndLoadData();
        return table;
    }

    public object[] ShredObject(DataTable table, T instance)
    {
        FieldInfo[] fi = _fi;
        PropertyInfo[] pi = _pi;
        if (instance.GetType() != typeof(T))
        {
            ExtendTable(table, instance.GetType());
            fi = instance.GetType().GetFields();
            pi = instance.GetType().GetProperties();
        }
        object[] values = new object[table.Columns.Count];
        FieldInfo[] array = fi;
        foreach (FieldInfo f in array)
        {
            values[_ordinalMap[f.Name]] = f.GetValue(instance);
        }
        PropertyInfo[] array2 = pi;
        foreach (PropertyInfo p in array2)
        {
            values[_ordinalMap[p.Name]] = p.GetValue(instance, null);
        }
        return values;
    }

    public DataTable ExtendTable(DataTable table, Type type)
    {
        FieldInfo[] fields = type.GetFields();
        foreach (FieldInfo f in fields)
        {
            if (!_ordinalMap.ContainsKey(f.Name))
            {
                DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name] : table.Columns.Add(f.Name, f.FieldType);
                _ordinalMap.Add(f.Name, dc.Ordinal);
            }
        }
        PropertyInfo[] properties = type.GetProperties();
        foreach (PropertyInfo p in properties)
        {
            if (!_ordinalMap.ContainsKey(p.Name))
            {
                DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name] : table.Columns.Add(p.Name, p.PropertyType);
                _ordinalMap.Add(p.Name, dc.Ordinal);
            }
        }
        return table;
    }
}

它看起来并不简短,但是可以工作。用法示例:

DataTable usaCustomers =
 northwindContext.Customers
    .Where(c => c.Country == "USA")
    .CopyToDataTable();