本文介绍了精致的"IN"子句不适用于多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我的模型中有一个字符串属性,其中包含来自 MultiSelectList @ Html.ListBox 的ID.如果我选择两个列表项,则我的属性值将如下所示: 0100,0500 .

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.

问题:

Dapper where子句仅适用于单个值:

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.

推荐答案

这是因为您不需要告诉Dapper使用括号().它会自己做的很好.试试这个:

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 });

这篇关于精致的"IN"子句不适用于多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:22