Can I make Entity Framework use sqlite's LIKE instead of instr()?

谁都会走 提交于 2019-12-11 09:45:56

问题


Currently using .NET Core 2.

I use a LINQ query similar to:

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

EF seems to translate this to:

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"

By using instr(), the comparison is case-sensitive although the PersonAddress.Text column is set to COLLATE NOCASE. If I modify the query above to use LIKE instead, the search is case-insensitive as I intend it.

Is it possible to force EF to use LIKE?


回答1:


You can use EF.Functions.Like introduced in EF Core 2.0:

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


来源:https://stackoverflow.com/questions/46897812/can-i-make-entity-framework-use-sqlites-like-instead-of-instr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!