小时。我正在尝试构建linq查询,动态生成查询以动态发送字段的自定义排序。
我是
的构建逻辑Expression<Func<string, int>> SpaceStringSortExpression = (a) => a.StartsWith(" ") ? 2 : 1;
此代码(SpaceStringSortExpression.ToString())的签名是“a =&gt; 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)
但我无法将结果传递给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;
我有(从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]]".
整个代码是:
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();
有人可以帮我吗?
简单的例子(仅按动态参数排序)工作正常:
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;
}
答案 0 :(得分:6)
问题在于:
ConditionalExpression t = Expression.IfThenElse(call, Expression.Constant(2), Expression.Constant(1));
成为Action类型的表达式,执行“then”和“else”的表达式,但不会按预期返回值。这与OrderBy方法所需的签名不匹配,因此它会抛出您看到的异常。你想要这个:
ConditionalExpression t = Expression.Condition(call, Expression.Constant(2), Expression.Constant(1));
Condition节点类型具有返回值。调试它的一个好方法是编写一个执行此操作的单元测试:
Expression<Func<bool,int>> expr = (a) => a ? 2 : 1
然后使用调试器检查生成的表达式树。希望有所帮助。