Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Breaking changes in EF Core 3.0. How can I compare strings without getting the warning CA1308?

Tags:

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?

like image 231
xavier Avatar asked Nov 25 '19 10:11

xavier


1 Answers

The solution, as PanagiotisKanavos commented, was to simply use a.Name == b. Easy and it works!

like image 159
xavier Avatar answered Nov 15 '22 04:11

xavier