Search code examples
entity-frameworksqliteentity-framework-coresql-likecase-insensitive

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


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?


Solution

  • 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%")));