如何在Dapper中使用IN

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

一般承認的答案

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


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因