.NET[C#]Dapper中如何实现SELECT * FROM TABLE WHERE Id IN (...) 的条件查询?

.NET 作者: Rector 发布时间: 2018-01-07 11:43:47 15阅读 0评论 0收藏 收藏本文

.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

转载请注明:图享网 » .NET[C#]Dapper中如何实现SELECT * FROM TABLE WHERE Id IN (...) 的条件查询?

分享扩散:

文章评论

获取验证码