我可以让实体框架使用sqlite' LIKE而不是instr()吗?

时间:2017-10-23 20:18:55

标签: entity-framework sqlite entity-framework-core sql-like case-insensitive

目前正在使用.NET Core 2.

我使用类似于:

的LINQ查询
data y /*output dataset*/;
  set x(rename = ( x = k )) nobs = nobs  ; /*renaming variable x to k for making calculations */
    do i = 1 to _n_+1 ; /*iterating from 1 to current obseration in PDV + 1 */
       if  mod(i , 2) = 0 /*checking if observation is even or not*/ then do ;
          set x point = i ; /*setting same source dataset with POINT so that we can slice the dataset on even observation.
                              It will leave only required value for our next statement.*/
             y = x  ; /*assigning variable x to y so that we can calculate the difference.*/
             diff = k - y ;
       end ;
    end ;
  if mod(_n_, 2) ne 0 then output ;
run ;

EF似乎将其翻译为:

var peopleInRoseStreet = context.Person
    .Include(p => p.Addresses)
    .Where(p => p.Addresses.Any(a => a.Text.Contains("rose")));

通过使用SELECT "p".* FROM "Person" AS "p" WHERE EXISTS ( SELECT 1 FROM "PersonAddress" AS "a" WHERE (instr("a"."Text", 'rose') > 0) AND ("p"."Id" = "a"."person_id")) ORDER BY "p"."Id" ,虽然PersonAddress.Text列设置为instr(),但比较区分大小写。如果我修改上面的查询来改为使用COLLATE NOCASE,那么搜索就像我想要的那样不区分大小写。

是否可以强制EF使用LIKE

1 个答案:

答案 0 :(得分:5)

您可以使用EF Core 2.0中引入的EF.Functions.Like

var peopleInRoseStreet = context.Person
    .Include(p => p.Addresses)
    .Where(p => p.Addresses.Any(a => EF.Functions.Like(a.Text, "%rose%")));