Skip to content

Commit

Permalink
Merge pull request #3 from dnyaneshwar-surywanshi/patch-1
Browse files Browse the repository at this point in the history
I want To make the "Contains" method become case-insensitive so I hav…
  • Loading branch information
zHaytam authored Nov 14, 2021
2 parents 6e6f28e + 38838be commit 5060c49
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions DynamicExpressions.UnitTests/PredicateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void GetPredicate_ShouldHandleEqualsGenericOperators<T>(int id, T title,
[InlineData(3, "Title 3", FilterOperator.NotContains, "5")]
[InlineData(4, "Title 4", FilterOperator.StartsWith, "Title")]
[InlineData(5, "Title 5", FilterOperator.EndsWith, "5")]
[InlineData(5, "Title 6", FilterOperator.ContainsIgnoreCase, "title")]
public void GetPredicate_ShouldHandleNestedStringOperators(int id, string title, FilterOperator op, object value)
{
var entry = new Entry<string>(id, new SubEntry<string>(title));
Expand Down
4 changes: 3 additions & 1 deletion DynamicExpressions/DynamicExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public static class DynamicExpressions

private static readonly MethodInfo _stringContainsMethod = typeof(string).GetMethod("Contains"
, new Type[] { typeof(string) });
private static readonly MethodInfo _stringContainsMethodIgnoreCase = typeof(string).GetMethod("Contains"
, new Type[] { typeof(string), typeof(StringComparison) });
private static readonly MethodInfo _enumerableContainsMethod = typeof(Enumerable).GetMethods().Where(x => string.Equals(x.Name, "Contains", StringComparison.OrdinalIgnoreCase)).Single(x => x.GetParameters().Length == 2).MakeGenericMethod(typeof(string));

private static readonly MethodInfo _dictionaryContainsKeyMethod = typeof(Dictionary<string, string>).GetMethods().Where(x => string.Equals(x.Name, "ContainsKey", StringComparison.OrdinalIgnoreCase)).Single();
private static readonly MethodInfo _dictionaryContainsValueMethod = typeof(Dictionary<string, string>).GetMethods().Where(x => string.Equals(x.Name, "ContainsValue", StringComparison.OrdinalIgnoreCase)).Single();

Expand Down Expand Up @@ -61,6 +62,7 @@ private static Expression CreateFilter(MemberExpression prop, FilterOperator op,
FilterOperator.Equals => RobustEquals(prop, constant),
FilterOperator.GreaterThan => Expression.GreaterThan(prop, constant),
FilterOperator.LessThan => Expression.LessThan(prop, constant),
FilterOperator.ContainsIgnoreCase => Expression.Call(prop, _stringContainsMethodIgnoreCase, PrepareConstant(constant), Expression.Constant(StringComparison.OrdinalIgnoreCase)),
FilterOperator.Contains => GetContainsMethodCallExpression(prop, constant),
FilterOperator.NotContains => Expression.Not(GetContainsMethodCallExpression(prop, constant)),
FilterOperator.ContainsKey => Expression.Call(prop, _dictionaryContainsKeyMethod, PrepareConstant(constant)),
Expand Down
1 change: 1 addition & 0 deletions DynamicExpressions/FilterOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum FilterOperator
NotContains,
StartsWith,
EndsWith,
ContainsIgnoreCase,
IsEmpty,
IsNotEmpty,
ContainsKey,
Expand Down

0 comments on commit 5060c49

Please sign in to comment.