String comparison exception in Entity Framework FirstOrDefault

I am querying a table using Entity Framework. The first bit of code was what I wrote, the second bit was that ReSharper suggested that I reorganize it too. The first of these gracefully returns zero if it does not exist, but the second throws an exception.

This was done using records 0-1 in the table (all columns are marked as NOT NULL)

Code that works:

context.brandlink.FirstOrDefault(x => x.ManufacturerKey.ToLower() == manufacturerKey.ToLower()); 

and code that doesn't work:

 context.brandlink.FirstOrDefault(x => String.Equals(x.ManufacturerKey, manufacturerKey, StringComparison.InvariantCultureIgnoreCase)); 

Exception thrown:

The number of arguments provided to call the 'Boolean Equals (System.String, System.String, System.StringComparison)' method is incorrect

So my question is: what is the difference between the two expressions?

+5
source share
1 answer

So my question is: what is the difference between the two expressions?

Differentiation consists in the fact that the CLR String.Equals Method (String, String, StringComparison) is used later, which is not supported by EF according to to The CLR method for canonical mapping of functions , while all methods used in the first ( string.ToLower and string equality operator) are supported.

In general, you cannot control the comparison of stegs for EF requests from code, because they are controlled by the database.

+9
source

Source: https://habr.com/ru/post/1244920/


All Articles