.NET[C#]Dapper中如何实现SELECT * FROM TABLE WHERE Id IN (…) 的条件查询?
问题描述
比如有如下查询语句:
SELECT *
FROM Table
WHERE Id IN (...)
其中,WHERE查询条件中 IN 的参数怎样赋值?
方案一
string sql = "SELECT * FROM SomeTable WHERE id IN @ids"
var results = conn.Query(sql, new { ids = new[] { 1, 2, 3, 4, 5 }});
方案二
connection.Query<int>(
@"select *
from (select 1 as Id union all select 2 union all select 3) as X
where Id in @Ids",
new { Ids = new int[] { 1, 2, 3 });
转换出来的SQL语句为:
select *
from (select 1 as Id union all select 2 union all select 3) as X
where Id in (@Ids1, @Ids2, @Ids3)
// @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3
版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册