我有以下代码,这些代码根据我的LambdaExpression
输入在运行时生成SearchTerm
。我正在尝试构建动态的where
子句。但是,我坚持如何从LambdaExpression
转换为Expression<Func<T,bool>>
private static Expression<Func<T,bool>> GetSearchAppliedQuery(IEnumerable<SearchTerm> terms)
{
var parameterExpression = ExpressionHelper.Parameter<T>();
Expression finalExpression = Expression.Constant(true);
Expression subExpression = Expression.Constant(false);
// Build up the LINQ Expression backwards:
// query = query.Where(x => x.Property == "Value" && (x.AnotherProperty == "Value" || x.SomeAnotherProperty == "Value"));
foreach (var term in terms)
{
var hasMultipleTerms = term.EntityName?.Contains(',') ?? false;
if (hasMultipleTerms)
{
var entityTerms = term.EntityName.Split(',');
foreach (var entityTerm in entityTerms)
{
term.EntityName = entityTerm;
// x => x.Property == "Value" || x.AnotherProperty == "Value"
subExpression = Expression.OrElse(subExpression, GetComparisonExpression(term, parameterExpression));
}
}
// x => x.Property == "Value" && x.AnotherProperty == "Value"
finalExpression = Expression.AndAlso(finalExpression, hasMultipleTerms ? subExpression : GetComparisonExpression(term, parameterExpression));
}
// x => x.Property == "Value" && (x.AnotherProperty == "Value" || x.SomeAnotherProperty == "Value")
var lambdaExpression = ExpressionHelper.GetLambda<T, bool>(parameterExpression, finalExpression);
// How to do this conversion??
Expression<Func<T,bool>> returnValue = ..??;
return returnValue;
}
我正在尝试应用上述方法的结果来获取查询,如下所示:
public static IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T> specification)
{
var query = inputQuery;
// modify the IQueryable using the specification's criteria expression
if (specification.Criteria != null)
{
query = query.Where(specification.Criteria);
}
...
return query;
}
这样我的最终查询看起来就像
query = query.Where(x => x.Property == "Value" && (x.AnotherProperty == "Value" || x.SomeAnotherProperty == "Value"))
编辑1: 按照@Ivan Stoev
的要求添加ExpressionHelper.GetLambda
方法
public static class ExpressionHelper
{
public static LambdaExpression GetLambda<TSource, TDest>(ParameterExpression obj, Expression arg)
{
return GetLambda(typeof(TSource), typeof(TDest), obj, arg);
}
public static LambdaExpression GetLambda(Type source, Type dest, ParameterExpression obj, Expression arg)
{
var lambdaBuilder = GetLambdaFuncBuilder(source, dest);
return (LambdaExpression)lambdaBuilder.Invoke(null, new object[] { arg, new[] { obj } });
}
private static MethodInfo GetLambdaFuncBuilder(Type source, Type dest)
{
var predicateType = typeof(Func<,>).MakeGenericType(source, dest);
return LambdaMethod.MakeGenericMethod(predicateType);
}
}
我错过了一些非常基本的内容或做错了什么吗?请协助。
答案 0 :(得分:1)
用于获取lambda表达式的ExpressionHelper.GetLambda<T, bool>
方法隐藏了其实际类型,即所需的Expression<Func<T, bool>>
,因此您所需要的只是使用强制转换运算符:
return (Expression<Func<T, bool>>)lambdaExpression;
或者更好的方法是将ExpressionHelper.GetLambda<TSource, TDest>
的结果类型更改为Expression<Func<TSource, TDest>>
,或者不使用该辅助方法-当您在编译时知道泛型类型参数时,如果泛型仅使用一个Expression.Lambda个方法(ExpressionHelper.GetLambda<TSource, TDest>
等同于Expression.Lambda<Func<TSource, TDest>>
),例如
var lambdaExpression = Expression.Lambda<Func<T, bool>>(parameterExpression, finalExpression);