Skip to content

Commit

Permalink
FIX : protection contre un bug EF + evaluation client EF dans le plus…
Browse files Browse the repository at this point in the history
… cas simple

#236
#182
  • Loading branch information
Poltuu committed Oct 17, 2018
1 parent 55fbb11 commit 848b5d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/Rdd.Infra/Helpers/WebFilterConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,27 @@ protected Expression<Func<TEntity, bool>> AndFactory<TProp>(Func<TProp, Expressi
public Expression<Func<TEntity, bool>> Equals(IExpression field, IList values) => BuildLambda(Contains, field, values);
protected virtual Expression Contains(Expression leftExpression, IList values)
{
return Expression.Call(typeof(Enumerable), "Contains", new[] { leftExpression.Type }, Expression.Constant(values), leftExpression);
if (values.Count == 1)
{
return Expression.Equal(leftExpression, Expression.Constant(values[0]));
}
else
{
return Expression.Call(typeof(Enumerable), "Contains", new[] { leftExpression.Type }, Expression.Constant(values), leftExpression);
}
}

public Expression<Func<TEntity, bool>> NotEqual(IExpression field, IList values) => BuildLambda(NotEqual, field, values);
protected virtual Expression NotEqual(Expression leftExpression, IList values)
{
return Expression.Not(Contains(leftExpression, values));
if (values.Count == 1)
{
return Expression.NotEqual(leftExpression, Expression.Constant(values[0]));
}
else
{
return Expression.Not(Contains(leftExpression, values));
}
}

public Expression<Func<TEntity, bool>> ContainsAll(IExpression field, IList values) => AndFactory<object>(value => BuildLambda(Equal, field, value), values);
Expand Down
16 changes: 10 additions & 6 deletions test/Rdd.Web.Tests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ public void UntilOnDatetime()
Assert.Equal(users[1], nullUser);
}

[Fact]
public void EqualsFilter()
[Theory]
[InlineData(1)]
[InlineData(1, 2)]
public void EqualsFilter(params int[] values)
{
var builder = new WebFilterConverter<User>();
var expression = builder.Equals(PropertyExpression<User>.New(u => u.Id), new List<int> { 1, 2 });
var expression = builder.Equals(PropertyExpression<User>.New(u => u.Id), new List<int>(values));

var goodUser = new User { Id = 1 };
var badUser = new User { Id = 3 };
Expand All @@ -102,11 +104,13 @@ public void EqualsFilter()
Assert.Equal(users[0], goodUser);
}

[Fact]
public void NotEqualsFilter()
[Theory]
[InlineData(2)]
[InlineData(1, 2)]
public void NotEqualsFilter(params int[] values)
{
var builder = new WebFilterConverter<User>();
var expression = builder.NotEqual(PropertyExpression<User>.New(u => u.Id), new List<int> { 1, 2 });
var expression = builder.NotEqual(PropertyExpression<User>.New(u => u.Id), new List<int>(values));

var goodUser = new User { Id = 3 };
var badUser = new User { Id = 2 };
Expand Down

0 comments on commit 848b5d3

Please sign in to comment.