Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

482
Vistas
.Net Core 3.1 Linq to Entities - How to concat strings in where clause

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

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This looks like an case of Linq translating the query in a manner you aren't predicting.

Going from memory and no IDE on hand to check it, but give this a shot. If you split the full name on the space first, you can use the values in your query.

// returns an array based on the patter, if it can be split
var names = pattern.Split(" ");

// then use the array elements in the query
var users = context.User.Where(x => x.FirstName == pattern 
    || x.LastName == pattern
    || (x.FirstName == names[0] && x.LastName == names[1]));

The last OR condition of the query should then evaulate the 2 new elements in the names array, that was created off the pattern

over 4 years ago · Santiago Trujillo Denunciar

0

It seems a bug of MySql.Data.EntityFrameworkCore.

I use Pomelo.EntityFrameworkCore.MySql instead to solve the problem.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda