Skip to content

Commit

Permalink
Not(). Possibility to negate the whole predicate. (#136)
Browse files Browse the repository at this point in the history
* Not(). Possibility to negate the whole predicate.

Usage:
var predicate = PredicateBuilder.New<Product>(true);
predicate = predicate.And(x => x.Discontinued) // Discontinued == true
predicate = predicate.And(x => x.Id > 10) // Discontinued == true && Id > 10
predicate = predicate.Not(); // !(Discontinued == true && Id > 10)

I am currently using an implementation that uses Reflection, but this way is better.

* Request : Can you please use { and } for if-statements?

Co-authored-by: [email protected] <Lucas Diogo Deon>
  • Loading branch information
LucasDiogoDeon authored Feb 6, 2021
1 parent e438081 commit cac8b4e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/LinqKit.Core/ExpressionStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public Expression<Func<T, bool>> And([NotNull] Expression<Func<T, bool>> expr2)
return (IsStarted) ? _predicate = Predicate.And(expr2) : Start(expr2);
}

/// <summary>Not</summary>
public Expression<Func<T, bool>> Not()
{
if (IsStarted)
{
_predicate = Predicate.Not();
}
else
{
Start(x => false);
}
return _predicate;
}

/// <summary> Show predicate string </summary>
public override string ToString()
{
Expand Down
4 changes: 4 additions & 0 deletions src/LinqKit.Core/PredicateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public static Expression<Func<T, bool>> And<T>([NotNull] this Expression<Func<T,
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, expr2Body), expr1.Parameters);
}

/// <summary> NOT </summary>
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expr)
=> Expression.Lambda<Func<T, bool>>(Expression.Not(expr.Body), expr.Parameters);

/// <summary>
/// Extends the specified source Predicate with another Predicate and the specified PredicateOperator.
/// </summary>
Expand Down
49 changes: 49 additions & 0 deletions tests/LinqKit.Tests.Net452/PredicateBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,54 @@ public void PredicateBuilder_PredicateAndUsage()
var items = list.Where(predicate).ToList();
Assert.Empty(items);
}

[Fact]
public void PredicateBuilder_PredicateNot()
{
// Arrange
Expression<Func<string, bool>> expectedPredicate = s => s == "a";
expectedPredicate = expectedPredicate.Not();
var predicate = PredicateBuilder.New<string>(s => s == "a");

// Act
predicate.Not();

// Assert
var expected = expectedPredicate.Expand().ToString();
var actual = predicate.Expand().ToString();
Assert.Equal(expected, actual);
}

[Fact]
public void PredicateBuilder_PredicateNotAssignment()
{
// Arrange
Expression<Func<string, bool>> expectedPredicate = s => s == "a";
expectedPredicate = expectedPredicate.Not();
var predicate = PredicateBuilder.New<string>(s => s == "a");

// Act
predicate = predicate.Not();

// Assert
var expected = expectedPredicate.Expand().ToString();
var actual = predicate.Expand().ToString();
Assert.Equal(expected, actual);
}

[Fact]
public void PredicateBuilder_PredicateNotUsage()
{
// Arrange
var list = new List<string> { "a", "b", "c" };
var predicate = PredicateBuilder.New<string>(s => s == "a");

// Act
predicate = predicate.Not();

// Assert
var items = list.Where(predicate).ToList();
Assert.Equal(new[] { "b", "c", }, items);
}
}
}

0 comments on commit cac8b4e

Please sign in to comment.