动态Linq选择转换为IEnumerable [英] Dynamically Linq Select Cast To IEnumerable

查看:97
本文介绍了动态Linq选择转换为IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小部件构建器,该构建器将动态获取查询并返回包含结果的数据表.注意:这使用Dynamic Linq进行字符串查询,可在

I'm creating a Widget builder which Dynamically takes in Queries and returns a datatable with the results. NOTE: this uses Dynamic Linq to take in string queries the library source can be found here

我唯一的问题是将结果集转换为IEnumerable.

My only issue is casting the results set to an IEnumerable.

    public DataTable GetEntityData<D>(string Query, int NumbOfResults, List<string> Columns)
       where D : class
    {

        ObjectContext objectContext = ((IObjectContextAdapter)this).ObjectContext;

        var FDW = (objectContext.CreateObjectSet<D>() as IQueryable<D>).Where(Query).Take(NumbOfResults);

        string Column = "new(ID, ExtTitleID)";

        var res = FDW.Select(Column).Cast<object>().ToList();            

        return DataTableCaster.CreateTableObj(res);

    }

这是在第

    var res = FDW.Select(Column).Cast<object>().ToList();   

我收到错误消息无法将类型'DynamicClass1'强制转换为'System.Object'.LINQto Entities仅支持强制转换EDM基本类型或枚举类型."

I get The error "Unable to cast the type 'DynamicClass1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types."

它必须是匿名类型,这样我就可以获取与实体相关的属性,我不能使用反射将其转换为字符串列表,即

It must be an Anonymous Type so I can grab entity related properties I CANNOT cast this to a list of string using reflection ie

         // I cannot grab the correct Properties with this
         var FD = from p in FDW.ToList()
                 select
                 (
                   (
                    from col in Columns
                    select p.GetType().GetProperty(col).GetValue(p, null).ToString()
                   ).ToList()
                 ).ToList();

下面的代码无法获取内部类型的子属性.

the code below is unable to get subproperties of internal types.

推荐答案

我重写了Dynamic.cs类,以支持Ienumerable select而不是Iqueryable,它现在将支持我的演员表.要覆盖该类,请使用以下代码

I overrided the Dynamic.cs Class to suppor Ienumerable select instead of Iqueryable which now will support my cast. To Override the class use this code

    public static IEnumerable<T> Select<T>(this IEnumerable source, string selector, params object[] values)
    {
        return Select(source, selector, values).Cast<T>();
    }
    public static IEnumerable Select(this IEnumerable source, string selector, params object[] values)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (selector == null) throw new ArgumentNullException("selector");
        LambdaExpression lambda = DynamicExpression.ParseLambda(source.AsQueryable().ElementType, null, selector, values);
        return source.AsQueryable().Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Select",
                new Type[] { source.AsQueryable().ElementType, lambda.Body.Type },
                source.AsQueryable().Expression, Expression.Quote(lambda)));
    }

这篇关于动态Linq选择转换为IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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