Skip to content

Add notin operator #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void FiltersShouldWork()
IgnorePredefinedOrders = true
}).ToList();

var resultOfStringsComparisonTest = queryable.ApplyFilters(new DynamicQueryOptions
var resultOfStringsComparisonTest = queryable.ApplyFilters(new DynamicQueryOptions
{
Filters = new List<Filter>
{
Expand Down Expand Up @@ -417,5 +417,32 @@ public void CaseSensitivityShouldWork()
Assert.Empty(resultOfnoResultFilterWithCSDisabledAndInvalidCase);
Assert.True(stringsComparisonWithCaseSensitivity.Count == 0);
}

[Fact]
public void ShouldApplyNotInFilter()
{
var queryable = _ctx.Users.AsQueryable();

var notInFilter = new DynamicQueryOptions
{
Filters = new List<Filter>
{
new Filter
{
Operator = FilterOperation.NotIn,
PropertyName = "Name",
Value = "_1",
CaseSensitive = false
}
},
IgnorePredefinedOrders = true
};

var result = queryable.ApplyFilters(notInFilter).ToList();
Assert.NotEmpty(result);
Assert.Equal("Test_2", result[0].Name);
Assert.Equal(queryable.Count()-1, result.Count());
}
}

}
1 change: 1 addition & 0 deletions DynamicQueryBuilder.UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class TestBase
{ "eq", FilterOperation.Equals },
{ "lt", FilterOperation.LessThan },
{ "cts", FilterOperation.Contains },
{ "nin", FilterOperation.NotIn },
{ "ne", FilterOperation.NotEqual },
{ "ew", FilterOperation.EndsWith },
{ "sw", FilterOperation.StartsWith },
Expand Down
4 changes: 4 additions & 0 deletions DynamicQueryBuilder.UnitTests/TestData/FilterTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static IEnumerable<object[]> FilterQueryData
yield return new object[] { "x.Name.Contains(\"Test\")", FilterOperation.Contains, true, "Test", "Name" };
yield return new object[] { "x.Name.ToLowerInvariant().Contains(\"test\".ToLowerInvariant())", FilterOperation.Contains, false, "Test", "Name" };

// NotIn
yield return new object[] { "Not(x.Name.Contains(\"Test\"))", FilterOperation.NotIn, true, "Test", "Name" };
yield return new object[] { "Not(x.Name.ToLowerInvariant().Contains(\"test\".ToLowerInvariant()))", FilterOperation.NotIn, false, "Test", "Name" };

// NotEquals
// For checking strings
yield return new object[] { "(x.Name != \"Test\")", FilterOperation.NotEqual, true, "Test", "Name" };
Expand Down
6 changes: 6 additions & 0 deletions DynamicQueryBuilder/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static class ExpressionBuilder
{ "eq", FilterOperation.Equals },
{ "lt", FilterOperation.LessThan },
{ "cts", FilterOperation.Contains },
{ "nin", FilterOperation.NotIn },
{ "ne", FilterOperation.NotEqual },
{ "ew", FilterOperation.EndsWith },
{ "sw", FilterOperation.StartsWith },
Expand Down Expand Up @@ -517,6 +518,11 @@ internal static Expression BuildFilterExpression(ParameterExpression param, Filt
case FilterOperation.Contains:
return Expression.Call(parentMember, _stringContainsMethod, constant);

case FilterOperation.NotIn:
return parentMember.Type == typeof(string)
? Expression.Not(Expression.Call(parentMember, _stringContainsMethod, constant))
: Expression.Not(Expression.Call(compareToExpression, _stringContainsMethod, constant));

case FilterOperation.GreaterThan:
return parentMember.Type == typeof(string)
? Expression.GreaterThan(compareToExpression, comparisonConstant)
Expand Down
1 change: 1 addition & 0 deletions DynamicQueryBuilder/Models/Enums/FilterOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum FilterOperation
Equals,
LessThan,
Contains,
NotIn,
NotEqual,
EndsWith,
StartsWith,
Expand Down
3 changes: 3 additions & 0 deletions docs/dqb.org
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ In,
Equals,
LessThan,
Contains,
NotIn,
NotEqual,
EndsWith,
StartsWith,
Expand Down Expand Up @@ -429,6 +430,7 @@ DQB has default operation short codes for shorter HTTP queries which are below;
{ "eq", FilterOperation.Equals },
{ "lt", FilterOperation.LessThan },
{ "cts", FilterOperation.Contains },
{ "nin", FilterOperation.NotIn },
{ "ne", FilterOperation.NotEqual },
{ "ew", FilterOperation.EndsWith },
{ "sw", FilterOperation.StartsWith },
Expand All @@ -451,6 +453,7 @@ var mySettings = new DynamicQueryBuilderSettings
{ "my_eq", FilterOperation.Equals },
{ "my_lt", FilterOperation.LessThan },
{ "my_cts", FilterOperation.Contains },
{ "my_nin", FilterOperation.NotIn },
{ "my_ne", FilterOperation.NotEqual },
{ "my_ew", FilterOperation.EndsWith },
{ "my_sw", FilterOperation.StartsWith },
Expand Down