Created
May 26, 2016 19:19
-
-
Save philcleveland/ac8902802ecc54a680e45854fbfdc37f to your computer and use it in GitHub Desktop.
Trying to figure out how to build dapper dynamic query.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out https://github.com/StackExchange/dapper-dot-net/blob/master/Dapper.SqlBuilder/SqlBuilder.cs
Specifically the OrWhere()