如何在Dapper中使用IN

时间:2012-08-20 02:33:52

标签: c# dapper

我不确定这是否正确。有没有更好的方法来做到这一点:

public List<Category> ManyCategories(IEnumerable<int> cats)
{
     string categoriesJoined = string.Join(",", cats);
     using (var conn = new SqlConnection())
     {
          conn.Open();

          //make this a union all
          return _categories = conn.Query<Category>("select Id, ParentId, Name from [Meshplex].[dbo].Category where Id IN (@joined)", new { joined = categoriesJoined }).ToList();
      }
}

1 个答案:

答案 0 :(得分:6)

http://code.google.com/p/dapper-dot-net/#List_Support

 //string categoriesJoined = string.Join(",", cats);
 using (var conn = new SqlConnection())
 {
      conn.Open();

      //make this a union all
      return _categories = conn.Query<Category>("select Id, ParentId, Name from [Meshplex].[dbo].Category where Id IN @joined", new { joined = cats}).ToList();
  }