EF查询列表时,使用动态查询参数

-

在使用EF查询数据列表是,需要使用到动态参数...

1、新建ExpressionExtensions扩展类

public static class ExpressionExtensions
{
    public static Expression AndAlso(this Expression left, Expression right)
    {
        return Expression.AndAlso(left, right);
    }
    public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
    {
        return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
    }
    public static Expression Property(this Expression expression, string propertyName)
    {
        return Expression.Property(expression, propertyName);
    }
    public static Expression GreaterThan(this Expression left, Expression right)
    {
        return Expression.GreaterThan(left, right);
    }
    public static Expression<TDelegate> ToLambda<TDelegate>(this Expression body, params ParameterExpression[] parameters)
    {
        return Expression.Lambda<TDelegate>(body, parameters);
    }


    //多条件查询帮助类
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    //注意this
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
            (Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
            (Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
    }


    /// <summary>
    /// 去重复数据
    /// </summary>
    /// <typeparam name="TSource"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="source"></param>
    /// <param name="keySelector">用于去重的表达式,单个字段如:var query = people.DistinctBy(p => p.Id);多个字段如:var query = people.DistinctBy(p => new { p.Id, p.Name });</param>
    /// <returns></returns>
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source
        , Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
}

2、使用方法

Expression<Func<v_userinfo, bool>> queryUserParameters = p => true;
if (!string.IsNullOrEmpty(departmentcode))
  queryUserParameters = queryUserParameters.And(c => c.DepartmentCode == departmentcode);
if (!string.IsNullOrEmpty(classcode))
  queryUserParameters = queryUserParameters.And(c => c.ClassCode == classcode);
if (!string.IsNullOrEmpty(usertype))
  queryUserParameters = queryUserParameters.And(c => c.UserType == usertype);

db.Set<v_userinfo>().Where(whereLambda.Compile()).ToList();

.Compile(),非常重要,否则会报错。

image.png

本文转载 " 整理 "

原文地址 " "

相关文章!