/// <summary> /// Or连接条件 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="conditions">查询条件</param> public void OrIfNotEmpty <TEntity>(params Expression <Func <TEntity, bool> >[] conditions) { if (conditions == null) { return; } foreach (var condition in conditions) { if (condition == null) { continue; } if (Lambdas.GetConditionCount(condition) > 1) { throw new InvalidOperationException(string.Format(LibraryResource.OnlyOnePredicate, condition)); } if (string.IsNullOrWhiteSpace(Lambdas.GetValue(condition).SafeString())) { continue; } var predicate = _expressionResolver.Resolve(condition); if (predicate == null) { continue; } Or(predicate); } }
/// <summary> /// 设置查询条件 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="expression">查询条件表达式,如果参数值为空,则忽略该查询条件</param> public void WhereIfNotEmpty <TEntity>(Expression <Func <TEntity, bool> > expression) where TEntity : class { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } if (Lambdas.GetConditionCount(expression) > 1) { throw new InvalidOperationException(string.Format(LibraryResource.OnlyOnePredicate, expression)); } if (string.IsNullOrWhiteSpace(Lambdas.GetValue(expression).SafeString())) { return; } Where(expression); }
/// <summary> /// 获取查询条件表达式 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="predicate">查询条件,如果参数值为空,则忽略该查询条件,范例:t => t.Name == "",该查询条件被忽略。 /// 注意:一次仅能添加一个条件,范例:t => t.Name == "a" && t.Mobile == "123",不支持,将抛出异常</param> public static Expression <Func <TEntity, bool> > GetWhereIfNotEmptyExpression <TEntity>( Expression <Func <TEntity, bool> > predicate) where TEntity : class { if (predicate == null) { return(null); } if (Lambdas.GetConditionCount(predicate) > 1) { throw new InvalidOperationException(string.Format(LibraryResource.CanOnlyOneCondition, predicate)); } var value = predicate.Value(); if (string.IsNullOrWhiteSpace(value.SafeString())) { return(null); } return(predicate); }