Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
Implemented overload for substring
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Jun 1, 2020
1 parent 925bd9c commit 214a924
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void Apply_Filter_Single_Property_StartsWith()
}

[Fact]
public void Apply_Filter_Single_Property_Substring()
public void Apply_Filter_Single_Property_Substring_Int()
{
TestHelper.EnsureEDM();

Expand All @@ -132,6 +132,23 @@ public void Apply_Filter_Single_Property_Substring()
Assert.All(results, x => Assert.True(((dynamic)x).Description == "iPhone SE 64GB Blue"));
}

[Fact]
public void Apply_Filter_Single_Property_Substring_Int_Int()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$filter=substring(Description, 7, 2) eq 'SE'",
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Where(x => x.Description.Substring(7, 2) == "SE").Count(), results.Count);

Assert.All(results, x => Assert.True(((dynamic)x).Description.Substring(7, 2) == "SE"));
}

[Fact]
public void Apply_Filter_Single_Property_ToLower()
{
Expand Down
17 changes: 15 additions & 2 deletions Net.Http.OData/Query/Linq/FilterBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ internal static class FilterBinder

private static readonly MethodInfo s_stringStartsWith = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });

private static readonly MethodInfo s_stringSubstring = typeof(string).GetMethod("Substring", new[] { typeof(int) });
private static readonly MethodInfo s_stringSubstringInt = typeof(string).GetMethod("Substring", new[] { typeof(int) });

private static readonly MethodInfo s_stringSubstringIntInt = typeof(string).GetMethod("Substring", new[] { typeof(int), typeof(int) });

private static readonly MethodInfo s_stringToLower = typeof(string).GetMethod("ToLower", Type.EmptyTypes);

Expand Down Expand Up @@ -194,7 +196,18 @@ private static Expression Bind(FunctionCallNode functionCallNode)
return Expression.Call(Bind(functionCallNode.Parameters[0]), s_stringStartsWith, Bind(functionCallNode.Parameters[1]));

case "substring":
return Expression.Call(Bind(functionCallNode.Parameters[0]), s_stringSubstring, Bind(functionCallNode.Parameters[1]));
if (functionCallNode.Parameters.Count == 2)
{
return Expression.Call(Bind(functionCallNode.Parameters[0]), s_stringSubstringInt, Bind(functionCallNode.Parameters[1]));
}
else if (functionCallNode.Parameters.Count == 3)
{
return Expression.Call(Bind(functionCallNode.Parameters[0]), s_stringSubstringIntInt, Bind(functionCallNode.Parameters[1]), Bind(functionCallNode.Parameters[2]));
}
else
{
throw new NotSupportedException();
}

case "tolower":
return Expression.Call(Bind(functionCallNode.Parameters[0]), s_stringToLower);
Expand Down

0 comments on commit 214a924

Please sign in to comment.