Search code examples
ormlite-servicestack

ServiceStack ORM Lite custom sql LIKE statement wildcard


How do we use a LIKE with wildcards in a custom sql with servicestack ORMLite?

Following code does not seem to work:

var sql="SELECT TOP 10 Id,Value FROM SomeTable WHERE Value Like '%@term%'"
var results = Db.Select<CustomDTO>(sql, new {term = "stringToSearch"})

Solution

  • You need to add the wildcard to the param value, e.g:

    var sql = "SELECT Id,Value FROM SomeTable WHERE Value Like @term";
    var results = db.Select<SomeTable>(sql, new { term = "%foo%" });
    

    You can run this Live Example on Gistlyn to test it.