-6

i have a csv file with round about 10.000 lines in 20 columns. I need to read this file into a C# object to be able to do LINQ queries on this object. What would you prefer?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
STORM
  • 4,005
  • 11
  • 49
  • 98
  • 1
    Unless you have a long list of columns with complex data, and you need to handle many special cases, you can write a simple program on your own. Did you try anything? – Arghya C Nov 21 '15 at 19:17
  • 1
    can't you convert it to a db? – Amen Jlili Nov 21 '15 at 19:21
  • @Jelly: No i can not use a db. – STORM Nov 21 '15 at 19:30
  • @Arghya: I could read the csv line by line, create a class, and a list. But is this an effective way? – STORM Nov 21 '15 at 19:31
  • 1
    Note that on SO it would preferable not to have "library recommendation question" nor "what do you like" questions. – Alexei Levenkov Nov 21 '15 at 19:42
  • 1
    Why is my post getting downvoted? What do you expect? To those who have downvoted my question: TELL ME WHATS WRONG WITH MY QUESTION. – STORM Nov 21 '15 at 19:53
  • We expect you to do a little research (http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array), and try a few things. Also, you need to tell us what you're trying to do. "to be able to do LINQ queries on this object" means *nothing and everything*. How about giving us some ideas what you're trying to do with LINQ here. It may be clear in your head, but it's completely unclear outside of it. Maybe what you think you want to do is totally worthless, or maybe it's trivially achievable. There's SO MUCH we don't know. –  Nov 24 '15 at 17:27

1 Answers1

1

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;
  }
  reader.Close();
}

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

from data in ReadCSVFile(dataStream)
TheHans255
  • 2,059
  • 1
  • 19
  • 36