Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

411
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!