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

Commit 1e32864

Browse files
committed
fix sql contains and in expression visitors to allow for value types
1 parent 9f0b0e8 commit 1e32864

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpressionVisitor.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,27 +1061,14 @@ protected virtual object VisitArrayMethodCall(MethodCallExpression m)
10611061
var lambda = Expression.Lambda<Func<object>>(member);
10621062
var getter = lambda.Compile();
10631063

1064-
var inArgs = getter() as object[];
1064+
var inArgs = Sql.Flatten(getter() as IEnumerable);
10651065

10661066
StringBuilder sIn = new StringBuilder();
10671067
foreach (Object e in inArgs)
10681068
{
1069-
if (e.GetType().ToString() != "System.Collections.Generic.List`1[System.Object]")
1070-
{
1071-
sIn.AppendFormat("{0}{1}",
1072-
sIn.Length > 0 ? "," : "",
1073-
OrmLiteConfig.DialectProvider.GetQuotedValue(e, e.GetType()));
1074-
}
1075-
else
1076-
{
1077-
var listArgs = e as IList<Object>;
1078-
foreach (Object el in listArgs)
1079-
{
1080-
sIn.AppendFormat("{0}{1}",
1081-
sIn.Length > 0 ? "," : "",
1082-
OrmLiteConfig.DialectProvider.GetQuotedValue(el, el.GetType()));
1083-
}
1084-
}
1069+
sIn.AppendFormat("{0}{1}",
1070+
sIn.Length > 0 ? "," : "",
1071+
OrmLiteConfig.DialectProvider.GetQuotedValue(e, e.GetType()));
10851072
}
10861073

10871074
statement = string.Format("{0} {1} ({2})", quotedColName, "In", sIn.ToString());
@@ -1110,7 +1097,7 @@ protected virtual object VisitSqlMethodCall(MethodCallExpression m)
11101097
var lambda = Expression.Lambda<Func<object>>(member);
11111098
var getter = lambda.Compile();
11121099

1113-
var inArgs = getter() as object[];
1100+
var inArgs = Sql.Flatten(getter() as IEnumerable);
11141101

11151102
StringBuilder sIn = new StringBuilder();
11161103
foreach (Object e in inArgs)

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void Can_Selelct_using_chained_string_operations()
145145
}
146146

147147
[Test]
148-
public void Can_Select_using_Array_Contains()
148+
public void Can_Select_using_object_Array_Contains()
149149
{
150150
var vals = new object[]{ TestEnum.Val0, TestEnum.Val1 };
151151

@@ -160,6 +160,22 @@ public void Can_Select_using_Array_Contains()
160160
Assert.AreEqual(sql1, sql2);
161161
}
162162

163+
[Test]
164+
public void Can_Select_using_int_Array_Contains()
165+
{
166+
var vals = new int[] { (int)TestEnum.Val0, (int)TestEnum.Val1 };
167+
168+
var visitor1 = OrmLiteConfig.DialectProvider.ExpressionVisitor<TestType>();
169+
visitor1.Where(q => vals.Contains((int)q.EnumCol) || vals.Contains((int)q.EnumCol));
170+
var sql1 = visitor1.ToSelectStatement();
171+
172+
var visitor2 = OrmLiteConfig.DialectProvider.ExpressionVisitor<TestType>();
173+
visitor2.Where(q => Sql.In(q.EnumCol, vals) || Sql.In(q.EnumCol, vals));
174+
var sql2 = visitor2.ToSelectStatement();
175+
176+
Assert.AreEqual(sql1, sql2);
177+
}
178+
163179
private int MethodReturningInt(int val)
164180
{
165181
return val;

0 commit comments

Comments
 (0)