diff --git a/Dapper/SqlMapper.cs b/Dapper/SqlMapper.cs index 1a25b6ba9dbfdcbc9a2cf80675bbf89499941b05..2e29b8ecb029b1749686f8fe260ff1c0c1b67fb0 100644 --- a/Dapper/SqlMapper.cs +++ b/Dapper/SqlMapper.cs @@ -375,10 +375,17 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj command.Parameters.Add(listParam); } - command.CommandText = command.CommandText.Replace(namePrefix, - "(" + string.Join( - ",", Enumerable.Range(1, count).Select(i => namePrefix + i.ToString()) - ) + ")"); + if (count == 0) + { + command.CommandText = command.CommandText.Replace(namePrefix, "(SELECT NULL WHERE 1 = 0)"); + } + else + { + command.CommandText = command.CommandText.Replace(namePrefix, + "(" + string.Join( + ",", Enumerable.Range(1, count).Select(i => namePrefix + i.ToString()) + ) + ")"); + } } } diff --git a/Tests/Tests.cs b/Tests/Tests.cs index d2e2bfebaeebae75636af06a864020fe303d67b3..13b759c9c5bf8d4d2c27859138ab3fac3113937e 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -154,6 +154,9 @@ public void TestStringList() { connection.Query("select * from (select 'a' as x union all select 'b' union all select 'c') as T where x in @strings", new {strings = new[] {"a","b","c"}}) .IsSequenceEqualTo(new[] {"a","b","c"}); + + connection.Query("select * from (select 'a' as x union all select 'b' union all select 'c') as T where x in @strings", new { strings = new string[0] }) + .IsSequenceEqualTo(new string[0]); } public void TestExecuteCommand()