I am trying to filter a list by a search string. It says in the doc on the blue note that:
Contains
.IEnumerable
gives you the .NET Framework implementation of Contains
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.
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 .
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.
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.
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.
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.
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.
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.
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.
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.
You would be better off using LIKE
operator, e.g.
if (!String.IsNullOrEmpty(searchString))
{
customers = customers.Where(x => EF.Functions.Like(x.Email, $"%{searchString}%"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With