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

449
Vistas
LINQ - Sort by StartsWith and then Contains (AutoComplete)

I'm implementing a autocomplete feature where I need to filter SQL table based on User Input.

So, Requirement is to filter for the records where it should begin(sort) the results with StartsWith and then Contains.

Sample Data:

Dotnet Developer
Azure Administrator
Microsoft Azure
Azure DevOps
UX Developer
IOT Azure

If I start typing Azu, then it should show the results in the below order.

Azure Administrator
Azure DevOps
Microsoft Azure
IOT Azure

which means, we are looking for the results which startswith logic and then contains logic.

Currently, I have tried two approaches but those didn't work.

First Approach:

dbContext.dbTable.Select(h => h.FieldName.ToLowerInvariant()).Where(e => e.Contains(text)).OrderBy(s => s).Distinct().Take(count).ToList();  

Second Approach:

dbContext.dbTable.Where(e => e.FieldName.Contains(text)).OrderBy(h => h.FieldName.IndexOf(text)).ThenBy(c => c.FieldName.Length).Select(p => p.FieldName).Distinct().Take(count).ToList();  

Can anyone please guide me if this can be possible?

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

0

dbContext
  .dbTable
  .Where(x => x.FieldName.Contains(text))
  .Distinct()
  .OrderByDescending(x => x.FieldName.StartsWith(text))
  .Take(count);
  1. Filter only records that contain the search criteria (.Where(x => x.FieldName.Contains(text)))
  2. Sort those so that ones that begin with the search criteria are first (.OrderByDescending(x => x.FieldName.StartsWith(text)))
over 4 years ago · Santiago Trujillo Denunciar

0

Your main problem is the requirements or the understanding of the requirements.

Filter operation:
Reduce(=filter) the results based on a boolean expresssion where:
true ==> show the result
false ==> don't show.
It is achieved in linq, by the Where(...) clause.

In your case, the requirement is to Filter by the results that contain text.

.Where(e => e.Contains(text))

Sort Operation:
Given a list of results, decide on the order of them. Your requirement is to see the values that Start by text first.
In other words, to Sort by this rule.

String.StartsWith returns a boolean.
Running linq's OrderBy on booleans, orders them by false and then by true. This is because OrderBy default sorting is ascending, and false counts as "smaller" than true. (reasonable decision)

So, knowing this, you can achieve what you want by running

.OrderByDescending(s => s.StartsWith(text))

Full Logic:

dbContext.dbTable
         .Select(h => h.FieldName.ToLowerInvariant())
         .Where(e => e.Contains(text)) // filter
         .Distinct() // more filtering
         .OrderByDescending(s => s.StartsWith(text)) // sort 
         .Take(count)
         .ToList();  
over 4 years ago · Santiago Trujillo Denunciar

0

Try like the following

    dbContext.dbTable.Where(e => e.FieldName.Contains(text)).OrderBy(h => h.FieldName.ToLower().StartsWith(searchString.ToLower()) ? 0 : 1)
.ThenBy(c => c.FieldName.ToLower().Contains("order")).Select(p => p.FieldName).Distinct().Take(count).ToList();  
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