I have a user table with [FirstName] and [LastName] columns. I'm trying to build a search function that returns users that meet one of the criteria below:
- FirstName == myPattern, or
- LastName == myPattern, or
- FirstName LastName == myPattern
For example, if I have the following users in my database:
- Jack One
- Jack Two
- Jack Three
I'd like the function to return all of them when the input is Jack, but only return Jack One when the input is Jack One
I currently have the following code:
var users = context.User.Where(x => x.FirstName == pattern
|| x.LastName == pattern
|| x.FirstName + " " + x.LastName == pattern)
But this does not work as the it gets translated to the following query in MySQL
...WHERE (`p`.`firstName` = 'Jack One') OR (`p`.`lastName` = 'Jack One')) OR (((`p`.`firstName` + ' ') + `p`.`lastName`) = 'Jack One')
It does not work because I believe we need to use CONCAT(firstName, ' ', lastName) if I want to concat multiple strings in MySQL.
I tried using the following .NET functions but they cannot be translated to sql (The LINQ 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())
- string.Join(' ', firstName, lastName)
- string.Concat(firstName, " ", lastName)
How can I achieve this in .NET CORE 3.1 without pulling all data into memory and evaluating it in client?
Thanks