Skip to content

Commit 7302169

Browse files
jogibear9988StefH
authored andcommitted
Support Binary & For String and Int (#150)
1 parent 45c0d19 commit 7302169

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ Expression ParseLogicalAndOrOperator()
411411
switch (op.Id)
412412
{
413413
case TokenId.Amphersand:
414+
int parseValue;
415+
if (left.Type == typeof(string) && left.NodeType == ExpressionType.Constant && int.TryParse((string)((ConstantExpression)left).Value, out parseValue) && TypeHelper.IsNumericType(right.Type))
416+
{
417+
left = Expression.Constant(parseValue);
418+
}
419+
else if (right.Type == typeof(string) && right.NodeType == ExpressionType.Constant && int.TryParse((string)((ConstantExpression)right).Value, out parseValue) && TypeHelper.IsNumericType(left.Type))
420+
{
421+
right = Expression.Constant(parseValue);
422+
}
423+
414424
// When at least one side of the operator is a string, consider it's a VB-style concatenation operator.
415425
// Doesn't break any other function since logical AND with a string is invalid anyway.
416426
if (left.Type == typeof(string) || right.Type == typeof(string))

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

+17
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,23 @@ public void ExpressionTests_StringConcatenation()
14021402
Assert.Equal("FirstNamex y", resultAddWithAmpAndParams.First());
14031403
}
14041404

1405+
[Fact]
1406+
public void ExpressionTests_BinaryAnd()
1407+
{
1408+
// Arrange
1409+
var baseQuery = new[] { new { Int = 5 } }.AsQueryable();
1410+
1411+
// Act
1412+
var resultAddWithAmp = baseQuery.Select<int>("it.Int & @0", "4");
1413+
var resultAddWithAmp2 = baseQuery.Select<int>("it.Int & @0", "2");
1414+
var resultAddWithAmp3 = baseQuery.Select<int>("@0 & it.Int", "4");
1415+
1416+
// Assert
1417+
Assert.Equal(4, resultAddWithAmp.First());
1418+
Assert.Equal(0, resultAddWithAmp2.First());
1419+
Assert.Equal(4, resultAddWithAmp3.First());
1420+
}
1421+
14051422
[Fact]
14061423
public void ExpressionTests_Subtract_Number()
14071424
{

0 commit comments

Comments
 (0)