执行LambdaExpression时遇到麻烦

Ser*_*vik 3 linq expression-trees

H.我正在尝试构建linq查询,动态生成动态发送字段的自定义排序查询.

我是建构逻辑

Expression<Func<string, int>> SpaceStringSortExpression = (a) => a.StartsWith(" ") ? 2 : 1; 
Run Code Online (Sandbox Code Playgroud)

此代码(SpaceStringSortExpression.ToString())的签名是"a => IIF(a.StartsWith(\"\"),2,1)"

为了做到这一点,我做了:

ParameterExpression parameter = Expression.Parameter(typeof(TSource), "p1");
            Expression orderByProperty = Expression.Property(parameter, propertyName);
            ConstantExpression c = Expression.Constant(" ", typeof(string));
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            Expression call = Expression.Call(orderByProperty, mi, c);
            Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(call, parameter);
            ConditionalExpression t = Expression.IfThenElse(call, Expression.Constant(2), Expression.Constant(1));
            //t.tostring() - IIF(p1.Login.StartsWith(" "), 2, 1)
            LambdaExpression callt = Expression.Lambda(t, new[] { parameter });
            //callt.tostring() = p1 => IIF(p1.Login.StartsWith(" "), 2, 1)
Run Code Online (Sandbox Code Playgroud)

但是我无法将结果传递给OrderBy

MethodInfo genericMethod;

            genericMethod = OrderByMethod.MakeGenericMethod

            genericMethod = OrderByDescendingMethod.MakeGenericMethod
           (new[] { typeof(TSource), typeof(Int32) });

        object ret = genericMethod.Invoke(null, new object[] { source, callt });
        return (IQueryable<TSource>)ret;
Run Code Online (Sandbox Code Playgroud)

我有(从locolised IIS翻译)

Unable to convert "System.Linq.Expressions.Expression`1[System.Action`1[XXXX.User]]" to type "System.Linq.Expressions.Expression`1[System.Func`2[XXXX.User,System.Int32]]".
Run Code Online (Sandbox Code Playgroud)

整个代码是:

public static IQueryable<TSource> OrderByPropertyRegardingWhiteSpaces<TSource>
        (this IQueryable<TSource> source, string propertyName, bool ascDirection = true)
        {
  ParameterExpression parameter = Expression.Parameter(typeof(TSource), "p1");
            Expression orderByProperty = Expression.Property(parameter, propertyName);
            ConstantExpression c = Expression.Constant(" ", typeof(string));
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            Expression call = Expression.Call(orderByProperty, mi, c);
            Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(call, parameter);
            ConditionalExpression t = Expression.IfThenElse(call, Expression.Constant(2), Expression.Constant(1));
            //t.tostring() - IIF(p1.Login.StartsWith(" "), 2, 1)
            LambdaExpression callt = Expression.Lambda(t, new[] { parameter });
            //callt.tostring() = p1 => IIF(p1.Login.StartsWith(" "), 2, 1)
MethodInfo genericMethod;
genericMethod = OrderByMethod.MakeGenericMethod
                (new[] { typeof(TSource), typeof(Int32) });
object ret = genericMethod.Invoke(null, new object[] { source, callt });
            return (IQueryable<TSource>)ret;
   }



        private static readonly MethodInfo OrderByMethod =
        typeof(Queryable).GetMethods()
            .Where(method => method.Name == "OrderBy")
            .Where(method => method.GetParameters().Length == 2)
            .Single();
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我这个吗?

简单的例子(只是按动态参数排序)工作正常:

public static IQueryable<TSource> OrderByProperty<TSource>
        (this IQueryable<TSource> source, string propertyName, bool ascDirection = true)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(TSource), "p1");
            Expression orderByProperty = Expression.Property(parameter, propertyName);

            LambdaExpression call = Expression.Lambda(orderByProperty, new[] { parameter });
MethodInfo genericMethod;
            if (ascDirection)
            {
                genericMethod = OrderByMethod.MakeGenericMethod
                (new[] { typeof(TSource), orderByProperty.Type });
            }
            else
            {
                genericMethod = OrderByDescendingMethod.MakeGenericMethod
               (new[] { typeof(TSource), orderByProperty.Type });
            }
            object ret = genericMethod.Invoke(null, new object[] { source, call });
            return (IQueryable<TSource>)ret;
        }
Run Code Online (Sandbox Code Playgroud)

nsl*_*wes 6

问题是这个:

ConditionalExpression t = Expression.IfThenElse(call, Expression.Constant(2), Expression.Constant(1));
Run Code Online (Sandbox Code Playgroud)

成为Action类型的表达式,执行"then"和"else"的表达式,但不会按预期返回值.这与OrderBy方法所需的签名不匹配,因此它会抛出您看到的异常.你要这个:

ConditionalExpression t = Expression.Condition(call, Expression.Constant(2), Expression.Constant(1));
Run Code Online (Sandbox Code Playgroud)

Condition节点类型具有返回值.调试它的一个好方法是编写一个执行此操作的单元测试:

Expression<Func<bool,int>> expr = (a) => a ? 2 : 1
Run Code Online (Sandbox Code Playgroud)

然后使用调试器检查生成的表达式树.希望有所帮助.