I'm trying to achieve a compiled SQL query from LINQ which will check if query
is substring of any of three columns in database (case insensitive).
I'm using .NET Core 1.1
Query that I come up with is as follows:
users.Select(u => new
{
User = u,
query = u.FirstName.ToLower() + u.LastName.ToLower() + u.Email.ToLower()
}).Where(x => x.query.Contains(query))
But when looking at debug informations I am getting this warning:
The LINQ expression '(([u].FirstName.ToLower() + [u].LastName.ToLower()) + [u].Email.ToLower()).Contains(__query_0)' could not be translated and will be evaluated locally.
Second query that I tried:
users.Where(x => u.FirstName.ToLower().Contains(query) || u.LastName.ToLower().Contains(query) || u.Email.ToLower().Contains(query))
but it gives me exactly the same warning.
Why is is the case? I am looking for something like:
SELECT * FROM USERS WHERE FirstName LIKE query OR LastName LIKE query OR Email LIKE query
UPDATE
I did one more experiment :
users.Where(u =>
u.FirstName.Contains(query) ||
u.LastName.Contains(query) ||
u.Email.Contains(query));
And this also resulted in
The LINQ expression '(([u].FirstName.Contains(__query_0) OrElse [u].LastName.Contains(__query_1)) OrElse [u].Email.Contains(__query_2))' could not be translated and will be evaluated locally.
xxx_CI
you don't need the.ToLower()
.SELECT CONVERT (varchar, SERVERPROPERTY('collation'));
to verify.SQL_Latin1_General_CP1_CI_AS
. However I think (maybe I am mistaken) that when my query is not compiled to SQL it will be case sensitive.johnnymanjohn.nyman@company.com
. If the user is looking for "Johnny", he will get a hit on John Nyman, even though "Johnny" does not occur in either the first name, last name or email. Also note that you get different results if you swap the order of concatenation: putting the last name first (nymanjohnjohn.nyman@company.com
) suddenly yields no hits for "Johnny".