Skip to content

Instantly share code, notes, and snippets.

@philcleveland
Created May 26, 2016 19:19
Show Gist options
  • Save philcleveland/ac8902802ecc54a680e45854fbfdc37f to your computer and use it in GitHub Desktop.
Save philcleveland/ac8902802ecc54a680e45854fbfdc37f to your computer and use it in GitHub Desktop.
Trying to figure out how to build dapper dynamic query.
public Task<List<Tag>> SearchTags(params string[] searchTerms)
{
var sql = @"SELECT * FROM Tags WHERE ";
var term = "value LIKE %@t{0}%";
var sb = new StringBuilder(sql);
for (int i = 0; i < searchTerms.Length; i++)
{
sb.Append(string.Format(term, i + 1));
if(i != searchTerms.Length - 1)//islastterm
{
sb.Append(" OR ");
}
}
//e.g. SELECT * FROM studyTags WHERE value LIKE %@t1% OR value LIKE %@t2% OR value LIKE %@t3%
var tagSql = sb.ToString();
var tags = await _db.QueryAsync<Tag>(cn, new CommandDefinition(tagSql, new { /*What here?*/ }, tx));
return tags;
}
@philcleveland
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment