转自:https://codedefault.com/2018/does-dapper-support-the-like-operator-in-csharp-application
问题描述
如题,在.NET/C#的程序开发中,使用Dapper 查询数据时,如何实现类似SQL 查询语句中的like 操作,如:
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 应该如何处理呢?
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();
|