如題,在.NET/C#的程序開發中,使用Dapper
查詢數據時,如何實現相似SQL
查詢語句中的like
操做,如:php
var data = conn.Query(@"
select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like '%@T%'", new { T = (string)term });
以上的語句執行後的結果是錯誤的,在Dapper
中,相似SQL語句中的like
應該如何處理呢?sql
term = "whateverterm"; var encodeForLike = term => term.Replace("[", "[[]").Replace("%", "[%]"); string term = "%" + encodeForLike(term) + "%"; var data = conn.Query(@" select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like @term", new { term });
string query = "SELECT * from country WHERE Name LIKE CONCAT('%',@name,'%');" var results = connection.query<country>(query, new {name});
db.Query<Remitente>("SELECT * FROM Remitentes WHERE Nombre LIKE @n", new { n = "%" + nombre + "%" }) .ToList();