Home > Blockchain >  LINQ query to find elements from a list in a csv file
LINQ query to find elements from a list in a csv file

Time:09-07

I am trying to find if a any element of a first list is found for each line of a csv file

First list :

XX01235756777
YY01215970799

Second list (that would be the csv file) :

Column_1|Column_2|Column_3|Column_4|Column_5|Column_6|Column_7
VX|2022-06-09 11:50:55|Y|Y|N|TT56431254135|Microsoft
VX|2022-06-09 11:50:55|Y|Y|N|XX01235756777|Meta
VX|2022-06-09 11:50:55|Y|Y|N|YY18694654355|Nokia
VX|2022-06-09 11:50:55|Y|Y|N|OO01215970799|BlackStone
VX|2022-06-09 11:50:55|Y|Y|N|YY01215970799|Alphabet

My code attempt :

        List<string> filteredList = new List<string>();
        using (StreamReader reader = new StreamReader(csvFilePath))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Any(x => isinList.Contains(x.ToString())))
                {
                    filteredList.Add(line);
                }
            }
        }
        return filteredList;

filteredList should be :

VX|2022-06-09 11:50:55|Y|Y|N|XX01235756777|Meta
VX|2022-06-09 11:50:55|Y|Y|N|YY01215970799|Alphabet

I succeeded at finding a single element in each line of the csv file.

But I can't code the right LINQ to process if whole list is present in each line.

Any ideas or anywhere you can point me to would be a great help.

CodePudding user response:

What you have ends up splitting the line from the csv file into characters and each character is checked against the isinList. You should instead check if any string in the isinList exists in each line from the csv file.

List<string> filteredList = new List<string>();
using (StreamReader reader = new StreamReader(csvFilePath))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (isinList.Any(x => line.Contains(x)))
        {
            filteredList.Add(line);
        }
    }
}

CodePudding user response:

Using LINQ, and the File.ReadLines() to read the lines in the file, you can filter as follows:

var filteredLines = File.ReadLines(csvFilePath)
                            .Where(line => isinList.Any(isin => line.Contains(isin)))
                            .ToList();

OTOH, if isinList is very large, or the CSV file is very long, and you know Column_6 is the only possible match, you might gain performance by using a HashSet<string> and testing only Column_6:

var isinSet = isinList.ToHashSet();
var filteredLines = File.ReadLines(csvFilePath)
                        .Where(line => isinSet.Contains(line.Split('|')[5]))
                        .ToList();
  • Related