Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2d95fd7

Browse files
committed
Treat "null != column" the same way as "column != null" in SqlExpression
1 parent 27fec39 commit 2d95fd7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,14 @@ protected virtual object VisitBinary(BinaryExpression b)
12951295
}
12961296
}
12971297

1298+
if (left.ToString().Equals("null", StringComparison.OrdinalIgnoreCase))
1299+
{
1300+
// "null is x" will not work, so swap the operands
1301+
var temp = right;
1302+
right = left;
1303+
left = temp;
1304+
}
1305+
12981306
if (operand == "=" && right.ToString().Equals("null", StringComparison.OrdinalIgnoreCase))
12991307
operand = "is";
13001308
else if (operand == "<>" && right.ToString().Equals("null", StringComparison.OrdinalIgnoreCase))

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ public void Can_Select_using_Nullable_HasValue()
185185
CollectionAssert.AreEquivalent(new[] { 2 }, target.Select(t => t.Id).ToArray());
186186
}
187187

188+
[Test]
189+
public void Can_Select_using_constant_Yoda_condition()
190+
{
191+
var q = Db.From<TestType>().Where(x => null != x.NullableIntCol); // "null != x.NullableIntCol" should be the same as "x.NullableIntCol != null"
192+
var target = Db.Select(q);
193+
CollectionAssert.AreEquivalent(new[] { 1, 3, 4 }, target.Select(t => t.Id).ToArray());
194+
}
195+
188196
[Test]
189197
public void Can_Select_using_Startswith()
190198
{

0 commit comments

Comments
 (0)