Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ Expression ParseLogicalAndOrOperator()
switch (op.Id)
{
case TokenId.Amphersand:
int parseValue;
if (left.Type == typeof(string) && left.NodeType == ExpressionType.Constant && int.TryParse((string)((ConstantExpression)left).Value, out parseValue) && TypeHelper.IsNumericType(right.Type))
{
left = Expression.Constant(parseValue);
}
else if (right.Type == typeof(string) && right.NodeType == ExpressionType.Constant && int.TryParse((string)((ConstantExpression)right).Value, out parseValue) && TypeHelper.IsNumericType(left.Type))
{
right = Expression.Constant(parseValue);
}

// When at least one side of the operator is a string, consider it's a VB-style concatenation operator.
// Doesn't break any other function since logical AND with a string is invalid anyway.
if (left.Type == typeof(string) || right.Type == typeof(string))
Expand Down
17 changes: 17 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,23 @@ public void ExpressionTests_StringConcatenation()
Assert.Equal("FirstNamex y", resultAddWithAmpAndParams.First());
}

[Fact]
public void ExpressionTests_BinaryAnd()
{
// Arrange
var baseQuery = new[] { new { Int = 5 } }.AsQueryable();

// Act
var resultAddWithAmp = baseQuery.Select<int>("it.Int & @0", "4");
var resultAddWithAmp2 = baseQuery.Select<int>("it.Int & @0", "2");
var resultAddWithAmp3 = baseQuery.Select<int>("@0 & it.Int", "4");

// Assert
Assert.Equal(4, resultAddWithAmp.First());
Assert.Equal(0, resultAddWithAmp2.First());
Assert.Equal(4, resultAddWithAmp3.First());
}

[Fact]
public void ExpressionTests_Subtract_Number()
{
Expand Down