Skip to content

Commit

Permalink
Fix linq expression mapping on '!Contains'
Browse files Browse the repository at this point in the history
  • Loading branch information
lbnascimento committed Apr 13, 2020
1 parent 05b8b33 commit e2a7f20
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion LiteDB.Tests/Mapper/LinqBsonExpression_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ public void Linq_Enumerables()
TestExpr<User>(x => x.PhoneNumbers.Contains(1234), "PhoneNumbers ANY = @p0", 1234);
TestExpr<User>(x => x.Phones2.Contains(new Phone { Number = 1 }), "Phones2 ANY = { Number: @p0 }", 1);

// negated contains
TestExpr<User>(x => !x.PhoneNumbers.Contains(1234), "(PhoneNumbers ANY = @p0) = false", 1234);
TestExpr<User>(x => !x.Phones2.Contains(new Phone { Number = 1 }), "(Phones2 ANY = { Number: @p0 }) = false", 1);

// fixed position with filter expression
TestExpr<User>(x => x.Phones.First(p => p.Number == 1), "FIRST(FILTER($.Phones=>(@.Number=@p0)))", 1);

Expand All @@ -240,7 +244,7 @@ public void Linq_Predicate()
TestPredicate<User>(x => x.Active && !x.Active, "((Active = true) AND (Active = false))");
TestPredicate<User>(x => !x.Active, "(Active = false)");
TestPredicate<User>(x => !x.Active == true, "((Active = false) = @p0)", true);
TestPredicate<User>(x => !(x.Salary == 50), "(Salary = @p0) = false", 50);
TestPredicate<User>(x => !(x.Salary == 50), "((Salary = @p0)) = false", 50);

// test for precedence order
TestPredicate<User>(x => x.Name.StartsWith("J") == false, "(Name LIKE (@p0 + '%') = @p1)", "J", false);
Expand Down
2 changes: 2 additions & 0 deletions LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ protected override Expression VisitUnary(UnaryExpression node)
// otherwise, resolve all expression as inner expression = false
else
{
_builder.Append("(");
this.Visit(node.Operand);
_builder.Append(")");
_builder.Append(" = false");
}
}
Expand Down

1 comment on commit e2a7f20

@lbnascimento
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for #1613

Please sign in to comment.