dapper “IN” clause not working with multiple values

坚强是说给别人听的谎言 提交于 2021-02-08 13:51:56

问题


Scenario:

I have a string property in my model that holds the IDs from a MultiSelectList @Html.ListBox. If I select two list items, my property value will look like this 0100,0500.

The problem:

Dapper where clause will only work with a single value:

CODE IN (@SomeCode) // for example, 0100 or 0500 returns results
CODE IN (@SomeCode) // 0100,0500 does not return results.

回答1:


That's because you don't need to tell Dapper to use parenthesis (). It will do fine by its own. Try this:

var codes = new List<string> { "0100","0500"};
var sql = "select * from SomeTable where CODE IN @codes";
var items = connection.Query(sql, new { codes });


来源:https://stackoverflow.com/questions/31997381/dapper-in-clause-not-working-with-multiple-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!