Skip to content

Commit

Permalink
Fix String.Equals together with other condition (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 18, 2024
1 parent b183afc commit c3aaf0c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,17 +2065,17 @@ private bool TryParseEnumerable(Expression instance, Type elementType, string me

args = ParseArgumentList();

var t = type ?? instance.Type;
if (t == typeof(string) && _methodFinder.ContainsMethod(t, methodName, false, instance, ref args))
_it = outerIt;
_parent = oldParent;

var typeToCheckForTypeOfString = type ?? instance.Type;
if (typeToCheckForTypeOfString == typeof(string) && _methodFinder.ContainsMethod(typeToCheckForTypeOfString, methodName, false, instance, ref args))
{
// In case the type is a string, and does contain the methodName (like "IndexOf"), then return false to indicate that the methodName is not an Enumerable method.
expression = null;
return false;
}

_it = outerIt;
_parent = oldParent;


if (type != null && TypeHelper.IsDictionary(type) && _methodFinder.ContainsMethod(type, methodName, false))
{
var dictionaryMethod = type.GetMethod(methodName)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ public void DynamicExpressionParser_ParseLambda_RenameEmptyParameterExpressionNa
[Theory]
[InlineData(@"p0.Equals(""Testing"", 3)", "testinG", true)]
[InlineData(@"p0.Equals(""Testing"", StringComparison.InvariantCultureIgnoreCase)", "testinG", true)]
public void DynamicExpressionParser_ParseLambda_SupportEnumerationStringComparison(string expressionAsString, string testValue, bool expectedResult)
public void DynamicExpressionParser_ParseLambda_StringEquals(string expressionAsString, string testValue, bool expectedResult)
{
// Arrange
var p0 = Expression.Parameter(typeof(string), "p0");
Expand All @@ -1609,6 +1609,26 @@ public void DynamicExpressionParser_ParseLambda_SupportEnumerationStringComparis
Check.That(result).IsEqualTo(expectedResult);
}

// #799
[Theory]
[InlineData(@"UserName.Equals(""Testing"", 3) and Income > 0")]
[InlineData(@"UserName.Equals(""Testing"", StringComparison.InvariantCultureIgnoreCase) and Income > 0")]
[InlineData(@"Income > 0 && UserName.Equals(""Testing"", 3)")]
[InlineData(@"Income > 0 && UserName.Equals(""Testing"", StringComparison.InvariantCultureIgnoreCase)")]
public void DynamicExpressionParser_ParseLambda_StringEquals_WithCombinedCondition(string expressionAsString)
{
// Arrange
var u = Expression.Parameter(typeof(User), "u");

// Act
var expression = DynamicExpressionParser.ParseLambda(new[] { u }, typeof(bool), expressionAsString);
var del = expression.Compile();
var result = del.DynamicInvoke(new User { UserName = "testinG", Income = 10 }) as bool?;

// Assert
Check.That(result).IsEqualTo(true);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_0_Arguments()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,12 @@ public void CastToIntUsingParentheses()
}.AsQueryable();

// Act
var result = qry.Where("DisplayName.Any(int(it) >= 109)").ToDynamicArray<User>();
var result1 = qry.Where("DisplayName.Any(int(it) >= 109) and Id > 0").ToDynamicArray<User>();
var result2 = qry.Where("Id > 0 && DisplayName.Any(int(it) >= 109)").ToDynamicArray<User>();

// Assert
result.Should().HaveCount(1).And.Subject.First().Id.Should().Be(1);
result1.Should().HaveCount(1).And.Subject.First().Id.Should().Be(1);
result2.Should().HaveCount(1).And.Subject.First().Id.Should().Be(1);
}

[Fact]
Expand Down

0 comments on commit c3aaf0c

Please sign in to comment.