首页 > 技术文章 > Linq to Entity 动态拼接查询条件(重点是OR)

icebutterfly 2018-01-05 10:54 原文

  1. public static class PredicateExtensions
    {
    /// <summary>
    /// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效。即,设置为True时所有or语句应该放在and语句之后,否则无效
    /// </summary>
    public static Expression<Func<T, bool>> True<T>() { return f => true; }

    /// <summary>
    /// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效。 即,设置为False时所有or语句应该放在and语句之前,否则无效
    /// </summary>
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1,
    Expression<Func<T, bool>> expression2)
    {
    var invokedExpression = Expression.Invoke(expression2, expression1.Parameters
    .Cast<Expression>());

    return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression),
    expression1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
    Expression<Func<T, bool>> expression2)
    {
    var invokedExpression = Expression.Invoke(expression2, expression1.Parameters
    .Cast<Expression>());

    return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body,
    invokedExpression), expression1.Parameters);
    }
    }

  2. /// <summary>
    /// 获取指定type异常预警
    /// </summary>
    /// <param name="typeArray">整数数组</param>
    /// <returns></returns>
    public List<p2p_log_error> GetNewError(int[] typeArray)
    {
    var predicate = PredicateExtensions.False<p2p_log_error>();//设置为False,所有and条件都应该放在or之后,如where (type=1 or type=14) and status==0
    foreach (var t in typeArray)
    {
    int type_temp= t;
    predicate = predicate.Or(p => p.type == type_temp);
    }
    var result = entities.p2p_log_error.Where(predicate.Compile());
    return result.Where(p => p.status == 0).OrderByDescending(i => i.id).ToList();
    }

推荐阅读