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

Commit 221fafb

Browse files
authored
Merge pull request #523 from OlegNadymov/master
Added support "ToString" method in SqlExpression.
2 parents 1e223f0 + feb7e9b commit 221fafb

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
@@ -2178,6 +2178,16 @@ protected virtual object VisitColumnAccessMethod(MethodCallExpression m)
21782178
statement = GetSubstringSql(quotedColName, startIndex);
21792179
}
21802180
break;
2181+
case "ToString":
2182+
if (m.Object.Type == typeof(string))
2183+
{
2184+
statement = string.Format("({0})", quotedColName);
2185+
}
2186+
else
2187+
{
2188+
statement = string.Format("cast({0} as varchar(1000))", quotedColName);
2189+
}
2190+
break;
21812191
default:
21822192
throw new NotSupportedException();
21832193
}

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

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

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

0 commit comments

Comments
 (0)