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

Commit feb7e9b

Browse files
committed
Support "ToString" in VisitColumnAccessMethod.
1 parent 37f27d5 commit feb7e9b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,16 @@ protected virtual object VisitColumnAccessMethod(MethodCallExpression m)
21402140
statement = GetSubstringSql(quotedColName, startIndex);
21412141
}
21422142
break;
2143+
case "ToString":
2144+
if (m.Object.Type == typeof(string))
2145+
{
2146+
statement = string.Format("({0})", quotedColName);
2147+
}
2148+
else
2149+
{
2150+
statement = string.Format("cast({0} as varchar(1000))", quotedColName);
2151+
}
2152+
break;
21432153
default:
21442154
throw new NotSupportedException();
21452155
}

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,51 @@ public void Can_Where_using_filter_with_nested_properties()
413413
Assert.That(target.Count, Is.EqualTo(1));
414414
}
415415

416+
[Test]
417+
public void Can_Where_using_filter_with_ToString()
418+
{
419+
string filterText = "10";
420+
int filterInt = 10;
421+
422+
var q = Db.From<TestType>().Where(x => x.NullableIntCol.ToString() == filterText);
423+
var target = Db.Select(q);
424+
Assert.That(target.Count, Is.EqualTo(1));
425+
426+
q = Db.From<TestType>().Where(x => x.NullableIntCol.ToString() == null);
427+
target = Db.Select(q);
428+
Assert.That(target.Count, Is.EqualTo(1));
429+
430+
q = Db.From<TestType>().Where(x => x.NullableIntCol.ToString() == filterInt.ToString());
431+
target = Db.Select(q);
432+
Assert.That(target.Count, Is.EqualTo(1));
433+
434+
q = Db.From<TestType>().Where(x => x.NullableIntCol.ToString() == "NotNumeric");
435+
target = Db.Select(q);
436+
Assert.That(target.Count, Is.EqualTo(0));
437+
438+
q = Db.From<TestType>().
439+
Join<TestType2>().
440+
Where( x => x.NullableIntCol.ToString() == filterText && x.TestType2ObjCol.NullableIntCol.ToString() == filterText);
441+
target = Db.Select(q);
442+
Assert.That(target.Count, Is.EqualTo(1));
443+
444+
q = Db.From<TestType>().
445+
Join<TestType2>().
446+
Where(x => x.NullableIntCol.ToString() == filterText && x.TestType2ObjCol.NullableIntCol.ToString() == "NotNumeric");
447+
target = Db.Select(q);
448+
Assert.That(target.Count, Is.EqualTo(0));
449+
450+
var filterText2 = "qwer";
451+
452+
q = Db.From<TestType>().Where(x => x.TextCol.ToString() == filterText2);
453+
target = Db.Select(q);
454+
Assert.That(target.Count, Is.EqualTo(1));
455+
456+
q = Db.From<TestType>().Where(x => x.NullableIntCol.ToString().EndsWith("0"));
457+
target = Db.Select(q);
458+
Assert.That(target.Count, Is.EqualTo(3));
459+
}
460+
416461
private int MethodReturningInt(int val)
417462
{
418463
return val;

0 commit comments

Comments
 (0)