Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IN with Dapper

Tags:

c#

dapper

I am not sure if this is the right way or not. Is there a better way to do this:

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();
      }
}
like image 572
Luke101 Avatar asked Aug 20 '12 02:08

Luke101


1 Answers

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();
  }
like image 142
Charlie Avatar answered Oct 03 '22 06:10

Charlie