public void TestOrExpression () { Expression<Func<User, bool>> exp1 = u => u.FullName.Contains ("1"); Expression<Func<User, bool>> exp2 = u => u.FullName.Contains ("2"); Expression<Func<User, bool>> exp3 = u => u.FullName.Contains ("3"); var result = exp1.Or (exp2, exp3); output.WriteLine (result.ToString ()); var parameter = Expression.Parameter (typeof (User), "u"); result = new ParameterReplacer (parameter).Visit (result); var exp = Expression.Lambda<Func<User, bool>> (result, false, parameter); Assert.NotEmpty (UserQuery.Where (exp)); Assert.Equal (30, UserQuery.Where (exp).Count ()); }
public static MethodCallExpression GetWhereExpression<TSource> (this IQueryable<TSource> source, string searchKey, MethodInfo method, Expression<Func<TSource, string>> member, params Expression<Func<TSource, string>>[] others) { var parameter = GetParameterExpression (member); var logics = member.ToSearchLambda (method, searchKey) .Or (others.Select (m => m.ToSearchLambda (method, searchKey)).ToArray ()); logics = new ParameterReplacer (parameter).Visit (logics); var whereExpression = Expression.Lambda<Func<TSource, bool>> (logics, false, parameter); Debug.WriteLine (whereExpression); //生成 .where(u=>...) 方法调用表达式 return Expression.Call ( typeof (Queryable), nameof (Queryable.Where), new Type[] { source.ElementType }, source.Expression, whereExpression); }