Skip to content

Commit c3aaf0c

Browse files
authored
Fix String.Equals together with other condition (#800)
1 parent b183afc commit c3aaf0c

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2065,17 +2065,17 @@ private bool TryParseEnumerable(Expression instance, Type elementType, string me
20652065

20662066
args = ParseArgumentList();
20672067

2068-
var t = type ?? instance.Type;
2069-
if (t == typeof(string) && _methodFinder.ContainsMethod(t, methodName, false, instance, ref args))
2068+
_it = outerIt;
2069+
_parent = oldParent;
2070+
2071+
var typeToCheckForTypeOfString = type ?? instance.Type;
2072+
if (typeToCheckForTypeOfString == typeof(string) && _methodFinder.ContainsMethod(typeToCheckForTypeOfString, methodName, false, instance, ref args))
20702073
{
20712074
// 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.
20722075
expression = null;
20732076
return false;
20742077
}
2075-
2076-
_it = outerIt;
2077-
_parent = oldParent;
2078-
2078+
20792079
if (type != null && TypeHelper.IsDictionary(type) && _methodFinder.ContainsMethod(type, methodName, false))
20802080
{
20812081
var dictionaryMethod = type.GetMethod(methodName)!;

test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ public void DynamicExpressionParser_ParseLambda_RenameEmptyParameterExpressionNa
15951595
[Theory]
15961596
[InlineData(@"p0.Equals(""Testing"", 3)", "testinG", true)]
15971597
[InlineData(@"p0.Equals(""Testing"", StringComparison.InvariantCultureIgnoreCase)", "testinG", true)]
1598-
public void DynamicExpressionParser_ParseLambda_SupportEnumerationStringComparison(string expressionAsString, string testValue, bool expectedResult)
1598+
public void DynamicExpressionParser_ParseLambda_StringEquals(string expressionAsString, string testValue, bool expectedResult)
15991599
{
16001600
// Arrange
16011601
var p0 = Expression.Parameter(typeof(string), "p0");
@@ -1609,6 +1609,26 @@ public void DynamicExpressionParser_ParseLambda_SupportEnumerationStringComparis
16091609
Check.That(result).IsEqualTo(expectedResult);
16101610
}
16111611

1612+
// #799
1613+
[Theory]
1614+
[InlineData(@"UserName.Equals(""Testing"", 3) and Income > 0")]
1615+
[InlineData(@"UserName.Equals(""Testing"", StringComparison.InvariantCultureIgnoreCase) and Income > 0")]
1616+
[InlineData(@"Income > 0 && UserName.Equals(""Testing"", 3)")]
1617+
[InlineData(@"Income > 0 && UserName.Equals(""Testing"", StringComparison.InvariantCultureIgnoreCase)")]
1618+
public void DynamicExpressionParser_ParseLambda_StringEquals_WithCombinedCondition(string expressionAsString)
1619+
{
1620+
// Arrange
1621+
var u = Expression.Parameter(typeof(User), "u");
1622+
1623+
// Act
1624+
var expression = DynamicExpressionParser.ParseLambda(new[] { u }, typeof(bool), expressionAsString);
1625+
var del = expression.Compile();
1626+
var result = del.DynamicInvoke(new User { UserName = "testinG", Income = 10 }) as bool?;
1627+
1628+
// Assert
1629+
Check.That(result).IsEqualTo(true);
1630+
}
1631+
16121632
[Fact]
16131633
public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_0_Arguments()
16141634
{

test/System.Linq.Dynamic.Core.Tests/QueryableTests.Is,OfType,As,Cast.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,12 @@ public void CastToIntUsingParentheses()
508508
}.AsQueryable();
509509

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

513514
// Assert
514-
result.Should().HaveCount(1).And.Subject.First().Id.Should().Be(1);
515+
result1.Should().HaveCount(1).And.Subject.First().Id.Should().Be(1);
516+
result2.Should().HaveCount(1).And.Subject.First().Id.Should().Be(1);
515517
}
516518

517519
[Fact]

0 commit comments

Comments
 (0)