I had the following code, which was running well with EF Core 2.1:
.FirstOrDefault(a => (a.Name.Equals(b, StringComparison.InvariantCultureIgnoreCase)
.
(Ok, running well means I got the right results even if it was being evaluated in the client side and I didn't know it).
I updagred to EF Core 3.0 and I didn't get any error, but this code was not giving the expected results.
I saw here a solution. I tried a.Name.ToLower() == b.ToLower()
but then I got the the error:
Error CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'MyFunction(string, string)' with a call to 'string.ToLower(CultureInfo)'
If I use a ToLower(CultureInfo.InvariantCulture)
I get the message:
Error CA1308 In method 'MyFunction', replace the call to 'ToLower' with 'ToUpperInvariant'.
If I use ToUpperInvariant()
, then I get the error (I'm already aware of the LINQ breaking changes in EF Core 3.0):
The LINQ expression (... all the expression...) could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
So, I am the starting point.
Is there a way to both comply with CA1304 and run the query in the DB and not in the client side?
Equals
like this at all and the bug and warning will go away. String comparision in SQL is controlled by the column's collation. Indexes are created based on the column collation too. In most cases, tables are created using case-invariant collation which means you don't have to modify the case at all. – HeaddressName
is covered by an index, by changing your code to just.FirstOrDefault(a => a.Name. == b)
you could see N times faster execution, where N the number of rows in the table. The previous code locked the entire table too, which harms scalability – HeaddressStringComparison.InvariantCultureIgnoreCase
orToLower()
in the first place? Did you use a case-sensitive collation? Why not change it to case-insensitive instead? – Headdress.FirstOrDefault(a => (a.Name.Equals(b)))
, without theStringComparison.InvariantCultureIgnoreCase
and I just added it a couple of days ago to comply with CA1304. Should I just ignore this warning? – Dallona.Name == b
. – Headdress