带有sql server的ServiceStack ormlite:如何使用通配符获取where子句

时间:2016-08-25 19:25:52

标签: sql-server servicestack ormlite-servicestack

我们如何让servicestack的ormlite生成执行WHERE子句的SQL,如下所示:

WHERE FirstName like '%joe%' OR lastname like '%joe%'

目前,如果我这样做:

db.Select<Person>.Where(x=> x.LastName.Contains(searchstring) || x.FirstName.Contains(searchstring));

它将生成一个WHERE子句,如下所示,没有所需的通配符:

WHERE FirstName like 'joe' OR lastname like 'joe'

1 个答案:

答案 0 :(得分:3)

String包含OrmLite中的表达式按预期工作,如此Live example on Gistlyn中所示,它使用最新版本的OrmLite:

LogManager.LogFactory = new ConsoleLogFactory();

public class Person
{
    [AutoIncrement]
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

db.CreateTable<Person>();

db.Insert(new Person { FirstName = "Joeseph", LastName = "Smith" });

var rows = db.Select<Person>(x => 
    x.FirstName.Contains("joe") || x.LastName.Contains("joe"));

返回有效匹配项,并将执行的查询记录到控制台:

DEBUG: SQL: CREATE TABLE "Person" 
(
  "Id" INTEGER PRIMARY KEY AUTOINCREMENT, 
  "FirstName" VARCHAR(8000) NULL, 
  "LastName" VARCHAR(8000) NULL 
); 

DEBUG: SQL: INSERT INTO "Person" ("FirstName","LastName") VALUES (@FirstName,@LastName)
PARAMS: @FirstName=Joeseph, @LastName=Smith
DEBUG: SQL: SELECT "Id", "FirstName", "LastName" 
FROM "Person"
WHERE (upper("FirstName") like @0 OR upper("LastName") like @1)
PARAMS: @0=%JOE%, @1=%JOE%

OrmLite一直在使用参数化查询,因此您必须使用旧版本的OrmLite,如果升级到OrmLite的最新 v4.5.0 ,它应该按预期工作。