Read a CSV to a C# object to be able to do LINQ query on it [closed]

This part is pretty easy because you’re able to run LINQ queries on IEnumerable<T> – in other words, anything that you can do a foreach loop on is something that you can query. And there are a great many ways to do that.

First, however, I’m assuming that you already know the schema for the data you want to process (i.e., you already know that Column 17 is a birthdate or something like that). If you do, you will be able to build a single C# data object very quickly that your LINQ queries can easily run on. If you don’t have one, your C# data object will probably be an associative array instead (such as a HashMap with string keys).

If you have to run multiple queries on the data, you can simply read each line, one by one, and put it into a List<T> instance.

But if you have to pass through the CSV data only once, you can instead create an iterator method and only read out line by line:

public IEnumerable<DataObject> ReadCSVFile(Stream input)
  StreamReader reader = new StreamReader(input);
  string line;
  while ((line = reader.ReadLine()) != null) // this sets and checks line at the same time
    DataObject data = new DataObject();

    // ... fill in the parameters of your data object here ...

    // this will return the single data object,
    // let your LINQ query process it, and then continue the loop
    yield return data;

Then, when you do your LINQ query, you invoke the method, as in

from data in ReadCSVFile(dataStream)

Browse More Popular Posts

Leave a Comment