0

I'm trying to perform a search on a column inside a CSV file, but cannot for the life of me figure out the best way to do it. I'm quite new to C# so most things I found were quite complicated.

The CSV file has three columns "id", "title" and "rating", and I want to search trough the title column.

I cam across a suggestion that I should turn the CSV into a list and then search through it.

This is the code I have so far for converting the file into a list:

var reader = new StreamReader(File.OpenRead(@"books.csv"));
            List<string> listA = new List<string>();
            List<string> listB = new List<string>();
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(';');

                listA.Add(values[0]);
                listB.Add(values[1]);

                foreach (var column1 in listA)
                {
                    Console.WriteLine(column1);
                }

                foreach (var column2 in listA)
                {
                    Console.WriteLine(column2);
                }
            }

But I do not understand how it works and I get an error

Index was outside the bounds of the array

Could anybody point me in the right direction because I'm quite lost?

3
  • You are probably looking for 'using var sr = new StreamReader(fileName); string content = sr.ReadToEnd();' instead. But...The best solution really depends on several thing. How large is the file? Will you be performing multiple searches during a single execution of the code (in which case it may indeed be better to cache the source data in memory). How will the data be used? Do you need all data (id, title, ratings) for the entry you are searching for?
    – Barns
    Commented Sep 10, 2020 at 19:46
  • You can just read the file using CsvHelper. From that point I think you can figure out the rest. Commented Sep 10, 2020 at 19:46
  • I'm doing this for a school project so the data is not really complex. And the CSV Helper was quite useful, so thanks for that.
    – Gilbert
    Commented Sep 13, 2020 at 12:46

1 Answer 1

3

If you're using StreamReader, then you do not need to call File.OpenRead (as the StreamReader can handle this and it will alter the encoding of your data).

You're reading a csv (Comma-separated values) file, but you're doing a split on semi-colon. (You can check this, by opening your csv file in notepad).

Also you do a split, but you not check if there are 2 items. So values[1] can throw an Index was outside the bounds of the array error.

As a starting point try:

var listA = new List<string>();
var listB = new List<string>();

using (var reader = new StreamReader(@"books.csv"))
{
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        if (!string.IsNullOrEmpty(line))
        {
            var values = line.Split(',');

            if (values.Length > 1)
            {
                listA.Add(values[0]);
                listB.Add(values[1]);
            }
        }
    }
}

As per the comments. A better way to write the code will be to use a third party package like CSV Helper (https://joshclose.github.io/CsvHelper).

1
  • Hi Greg. I was finally able to load the data and search trough it, and the CSV Helper was indeed helpful. Thanks a lot for the help.
    – Gilbert
    Commented Sep 13, 2020 at 12:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.