Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dapper "IN" clause not working with multiple values

Tags:

dapper

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.
like image 981
Gregory Bologna Avatar asked Aug 13 '15 20:08

Gregory Bologna


1 Answers

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 });
like image 186
von v. Avatar answered Nov 18 '22 16:11

von v.