Entity Framework core - Contains is case sensitive or case insensitive?

前端 未结 6 2053
不思量自难忘°
不思量自难忘° 2020-12-25 12:30

\"Contains\" in Entity Framework core should equivalent to the SQL %like% operator. Therefore \"Contains\" should be case insensitive however it is case sensitive! (at least

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 13:03

    My answer will concern NpgSQL.

    1. EF.Functions.Like() in PostgreSQL is case-sensitive, but you can use EF.Functions.ILike() extension method located in Npgsql.EntityFrameworkCore.PostgreSQL assembly.

    2. If you don't have reference to Entity Framework assembly in place where you build query, you can use combination ToLower() and Contains() methods, because Npgsql is able translate ToLower() method to correct SQL

    Example:

    context.Counties.Where(x => x.Name.ToLower().Contains(keyword.ToLower())).ToList();
    

    About second method keep in mind: you may have performance problems and may encounter problems associated with encoding.

提交回复
热议问题