使用Dynamic.Linq,如何将查询返回到DataTable

时间:2015-03-30 08:11:14

标签: c# linq dynamic

我正在使用System.Linq.Dynamic

var query =     this.testFormsDataSet.Consignment.AsEnumerable().GroupBy(groupBy, "it")
.Select(selectBy);

使用该查询就可以了。

但是现在我不明白如何将查询保存到DataTable。我尝试使用https://msdn.microsoft.com/en-us/library/bb669096(v=vs.110).aspx

DataTable res = CustomLINQtoDataSetMethods.CopyToDataTable(query);

DataTable res = query.CopyToDataTable();

CopyToDataTable仍然无效。

请帮助!!

1 个答案:

答案 0 :(得分:0)

基于Convert IEnumerable to DataTable

public static class DynamicLinqDataTableHelpers
{
    public static DataTable ToDataTable(this IQueryable items)
    {
        Type type = items.ElementType;

        // Create the result table, and gather all properties of a type        
        DataTable table = new DataTable(type.Name);

        PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // Add the properties as columns to the datatable
        foreach (PropertyInfo prop in props)
        {
            Type propType = prop.PropertyType;

            // Is it a nullable type? Get the underlying type 
            if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                propType = Nullable.GetUnderlyingType(propType);
            }

            table.Columns.Add(prop.Name, propType);
        }

        // Add the property values as rows to the datatable
        foreach (object item in items)
        {
            var values = new object[props.Length];

            if (item != null)
            {
                for (var i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }
            }

            table.Rows.Add(values);
        }

        return table;
    }
}

然后

DataTable res = query.ToDataTable();

主要区别在于,而不是&#34;提取&#34;类型T到泛型,我使用ElementType

IQueryable属性

现在......你似乎使用了动态LINQ的一个奇怪变体,它适用于IEnumerable ...我已经改编了ToDataTable

public static class DynamicLinqDataTableHelpers
{
    public static DataTable ToDataTable(this IEnumerable items)
    {
        // Create the result table, and gather all properties of a type        
        DataTable table = new DataTable();

        PropertyInfo[] props = null;

        // Add the property values as rows to the datatable
        foreach (object item in items)
        {
            if (props == null && item != null)
            {
                Type type = item.GetType();
                props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

                // Add the properties as columns to the datatable
                foreach (PropertyInfo prop in props)
                {
                    Type propType = prop.PropertyType;

                    // Is it a nullable type? Get the underlying type 
                    if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        propType = Nullable.GetUnderlyingType(propType);
                    }

                    table.Columns.Add(prop.Name, propType);
                }
            }

            // When the column headers are defined, all the rows have
            // their number of columns "fixed" to the right number
            var values = new object[props != null ? props.Length : 0];

            if (item != null)
            {
                for (var i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }
            }

            table.Rows.Add(values);
        }

        return table;
    }
}