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

C# 使用Dynamic.Linq,如何将查询返回到DataTable,c#,linq,dynamic,C#,Linq,Dynamic,我正在使用System.Linq.Dynamic var query = this.testFormsDataSet.Consignment.AsEnumerable().GroupBy(groupBy, "it") .Select(selectBy); 有了这个疑问,一切都没问题 但现在我不明白如何将查询保存到DataTable。我试着用 或 但是CopyToDataTable仍然不起作用 请帮忙 基于 主要区别在于,我没有通过泛型“提取”类型T,而是使用IQueryable的Ele

我正在使用System.Linq.Dynamic

var query =     this.testFormsDataSet.Consignment.AsEnumerable().GroupBy(groupBy, "it")
.Select(selectBy);
有了这个疑问,一切都没问题

但现在我不明白如何将查询保存到DataTable。我试着用

但是
CopyToDataTable
仍然不起作用

请帮忙

基于

主要区别在于,我没有通过泛型“提取”类型T,而是使用
IQueryable的
ElementType
属性

现在。。。您似乎在
IEnumerable
上使用了一种奇怪的动态LINQ变体。。。我已将
调整为datatable

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;
    }
}
公共静态类DynamicLinqDataTableHelpers
{
公共静态数据表到数据表(此IEnumerable items)
{
//创建结果表,并收集类型的所有属性
DataTable=新的DataTable();
PropertyInfo[]props=null;
//将属性值作为行添加到datatable
foreach(项中的对象项)
{
if(props==null&&item!=null)
{
Type Type=item.GetType();
props=type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
//将属性作为列添加到datatable
foreach(PropertyInfo props in props)
{
类型propType=prop.PropertyType;
//它是可为null的类型吗?获取基础类型
if(propType.IsGenericType&&propType.GetGenericTypeDefinition()==typeof(可为空))
{
propType=Nullable.GetUnderlyingType(propType);
}
table.Columns.Add(prop.Name,propType);
}
}
//定义列标题后,所有行都具有
//它们的列数“固定”到正确的数目
变量值=新对象[props!=null?props.Length:0];
如果(项!=null)
{
对于(变量i=0;i
它说:“无法从用法中推断出方法“\uuuuuuuuuuuuuuuu”的类型参数。请尝试显式指定类型参数。”您可以尝试在
CopyToDataTable()
之前添加
Cast()
,但请记住
Select
的结果必须是
DataRow
(因为
CopyToDataTable
只在
DataRow
s上工作)什么.net框架?query.ToList()
能用吗?先试试看。@GertArnold query.ToList()不起作用…谢谢。我们今天会试试。仍然不起作用。“错误1实例参数:无法从'System.Collections.IEnumerable'转换为'System.Linq.IQueryable'”错误2'System.Collections.IEnumerable'不包含'ToDataTable'的定义,并且最佳扩展方法重载为'MyForm.DynamicClinQDataTableHelpers.ToDataTable(System.Linq.IQueryable)”“有一些是无效的arguments@Kavrat您的查询中有一些奇怪的地方:
this.testFormsDataSet.audition.AsEnumerable().GroupBy
,但我知道动态LINQ在
AsEnumerable()
上不起作用。请尝试删除
AsEnumerable()
@Kavrat好的……试着告诉我
this.testFormsDataSet
this.testFormsDataSet.authority的类型。如果您执行
var xx=this.testFormsDataSet
并在
var
上移动鼠标,您应该能够看到它。对
寄售执行相同的操作
this.testFormsDataSet.authority'-表示强名称的clas数据表“this.TestFormsDataSet”表示强类型内存缓存od数据。
DataTable res = query.CopyToDataTable();
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();
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;
    }
}