Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

414
Visualizações
How to search list of values in DataTable?

I have a DataTable with some n number of columns and array of string to search in this table. I want to search all these strings in DataTable and store matching strings in list.

Columns in DataTable are dynamic so using below code I got list of Columns from DataTable.

But not sure how to search and get matching records using LINQ or any other technique with best feasible approach.

DataColumn[] cols = dt.Columns.Cast<DataColumn>().ToArray();
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This should work:

DataColumn[] cols = dt.Columns.Cast<DataColumn>().ToArray();
var rows = dt.AsEnumerable();
List<string> foundList = searchList
    .Where(s => rows.Any(r => cols.Any(c => r[c].ToString().Equals(s))))
    .ToList();

But this would be more efficient:

HashSet<string> searchStrings = new HashSet<string>(searchList); // or use a HashSet<string> instead of a list in the first place
List<string> foundList = new List<string>();
foreach (DataRow row in dt.Rows)
{
    IEnumerable<string> matches = cols
       .Select(c => row[c].ToString())
       .Where(searchStrings.Contains); // Contains is O(1) operation
    foreach (string match in matches)
    {
        foundList.Add(match);
        searchStrings.Remove(match);   // Remove is O(1) operation. It has another advantage: at the end searchStrings contains only strings that were not found
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda