Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make contains case-insensitive in ef core 2?

I am trying to filter a list by a search string. It says in the doc on the blue note that:

  • IQueryable gives you the database provider implementation of Contains.
  • IEnumerable gives you the .NET Framework implementation of Contains
  • The default setting of SQL Server instances is case-insensitive.
  • Using ToUpper to make an explicit case-insensitive call should be avoided because it has a performance penalty.

My filtering is as follows:

IQueryable<ApplicationUser> customers = 
    from u in _context.Users
    where (u.Customer != null && u.IsActive)
    select u;

if (!string.IsNullOrEmpty(searchString))
{
    customers = customers.Where(s => s.Email.Contains(searchString));
}

This solution however is case-sensitive, and I don't really understand why: since I'm using IQueryable, it should use the database provider implementation, that is case-insensitive by default, right?

I'm using EF Core 2 and currently just running a local MSSQLLocalDB.

like image 847
J.Kirk. Avatar asked Jan 11 '18 22:01

J.Kirk.


People also ask

Is EF core case-sensitive?

For one thing, EF Core does know not which case-sensitive or case-insensitive collation should be used. More importantly, applying a collation would in most cases prevent index usage, significantly impacting performance for a very basic and commonly-used .

What is COLLATION case insensitive?

A case-insensitive collation ignores the differences between uppercase and lowercase letters for string comparison and sorting, whereas a case-sensitive collation does not. For example, in case-insensitive collation, “A” and “a” are equal.

How do you make a search case insensitive?

Case insensitive SQL SELECT: Use upper or lower functions select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

How do I make SQL Server database case-sensitive insensitive?

SQL Server is, by default case insensitive; however, it is possible to create a case sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine a database or database object is by checking its “COLLATION” property and look for “CI” or “CS” in the result.

Is Entity Framework Core - contains case sensitive or case insensitive?

Entity Framework core - Contains is case sensitive or case insensitive? "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 in postgres????) The following only outputs a result when the correct casing for keyword is used.

Is there a way to make contains case-insensitive in EF Core 2?

With reference to the answer above, no. There is no way to further load case sensitivity checks at an even lower level. You'll have to perform exactly what this person did: How do I make contains case-insensitive in ef core 2? Here's an example on the latest LINQ syntax.

Is EF like () case sensitive or case insensitive?

In all cases EF.Functions.Like () converts into a SQL LIKE expression which is case-insensitive by default unless db or column collation is defined otherwise.

How do I use case-sensitive collation with EF Core migrations?

When using EF Core migrations to manage your database schema, the following in your model's OnModelCreating method configures a SQL Server database to use a case-sensitive collation: Collations can also be defined on text columns, overriding the database default.


2 Answers

starting from version 2.1 of the EF Core, you can use HasConversion(). But the information in the database will be stored in lowercase:

builder.Property(it => it.Email).HasConversion(v => v.ToLowerInvariant(), v => v);

I solved a similar problem. This change solved all my problems.

like image 198
Alexander Brattsev Avatar answered Oct 21 '22 21:10

Alexander Brattsev


You would be better off using LIKE operator, e.g.

if (!String.IsNullOrEmpty(searchString))
{
    customers = customers.Where(x => EF.Functions.Like(x.Email, $"%{searchString}%"));
}
like image 40
zaitsman Avatar answered Oct 21 '22 22:10

zaitsman