Entity structure returns a different result with the predicate

string personName= "JoHn"; //(in my table iam already having a person named='john') Func<Person, bool> predicate = (p) => p.Name== personName; var res2 = dataContext.Persons.Any(predicate); //returns false var res1 = dataContext.Persons.Any(p=>p.Name== personName); // returns true 

I think that with a predicate, it considers the case property of the personName property, and without it, case simply ignores it.

Does anyone know why?

+4
source share
1 answer

A Func<Page, bool> is a delegate, which means that you are doing this in LINQ-to-Objects (i.e. in memory, in C #). .NET Strings case sensitive, so this applies to the case.

This version, however:

 var res1 = dataContext.Persons.Any(p=>p.Name== personName); 

uses IQueryable<T> and expression trees; it will be executed as a TSQL filter that will apply database rules. What happens here depends on how your database is configured (depending on the database, it may be case sensitive or case sensitive).

If you want them both to use the same logic, notice the difference here:

 Expression<Func<Page, bool>> predicate = (p) => p.Name== personName; var res2 = dataContext.Persons.Any(predicate); 

Adding Expression<...> makes this expression tree, not a delegate, so it is "composed" and executed in the database (via TSQL translation), just like:

 var res1 = dataContext.Persons.Any(p=>p.Name== personName); 
+7
source

All Articles