-1

I got some issue in parsing or reading the CSV. I want to achieve to pick the selected value in CSV file because it doesn't have a header. Then I want to store the selected item into the database.

Example Data:

Row 1: "","",John,"",28,"","", 
Row 2: "",Doe,"","USA",
Row 3: Devoper,"","","ABC Company",
Row 4: "","",10,000,"2 Years",
Row 5: "C#","LINQ","ASP.NET MVC",

I want to select like this:

Row 1: John,28
Row 2: Doe, USA
Row 3: Developer, ABC Company
Row 4: 10,000, 2 Years
Row 5: C#, LINQ, ASP.NET MVC

We assume class structured is like this:

public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Position { get; set; }
public double Salary { get; set; }
public string YearWorked { get; set; }
public string Skills { get; set; }
PAT
  • 41
  • 1
  • 6
  • how do you recognize which value goes where? Is First name always in forst row and on third position? – Nino Jun 05 '17 at 08:15
  • @Nino John is the First Name and Doe is the Last Name sir :) – PAT Jun 05 '17 at 08:17
  • That is not an answer to my question. What is the structure of your file? Is first name always on third place, or can it be on first or second? I'm asking for a file structure... – Nino Jun 05 '17 at 08:20
  • It can be on first or second Sir :) – PAT Jun 05 '17 at 08:21
  • @PatrickPangilinan Your problem is in no way related to programming, it is impossible to find out what the first name is if there is no indicator. You have a serious problem with your data structure, the easiest way to solve that is to open Excel and copy & paste all data by hand, and use named columns. – Snicker Jun 05 '17 at 08:26
  • @Snicker :( but do you know some tools or framework that going to be useful? – PAT Jun 05 '17 at 08:30
  • @JuanFerrer thank you for your suggestion, but that's not the right answer my data doesn't have a header at all. – PAT Jun 05 '17 at 08:34
  • as Snicker stated, this cannot be done. Basically, you want your program to guess values and then put them to corresponding properties. – Nino Jun 05 '17 at 08:34
  • @Nino yes you're right :) – PAT Jun 05 '17 at 08:35
  • @PatrickPangilinan Have you checked the [second answer](https://stackoverflow.com/a/20523165/7179042)? It's a generic way of parsing CSV into an array. I believe that's what you want, isn't it? – juanferrer Jun 05 '17 at 08:39
  • @Patrick Pangilinan: A true Artificial Intelligence could solve that. But we still are nowhere closer to those then we were 1950. That CSV structure seems to be totally FUBAR. CSV is supposed to be structured for easier Automated Processing, but this seems to be the opposite. You either need the rework the file structure by hand. Or go back to the data source and get a decently structured CSV instead of this. – Christopher Jun 05 '17 at 08:40
  • @JuanFerrer hmm .. good reference Sir, thank you :) – PAT Jun 05 '17 at 08:48
  • Can you post more samples of the input data. Files like this are easy to read, but if more than one record is in the file I need to see how the data repeats. If there are any blanks rows or if any fields get get skipped. – jdweng Jun 05 '17 at 09:23

1 Answers1

0

LINQ is a wonderfull quick tool. But like all quick tools, it has limits. I generally consider parsing CSV to be way beyond those limits.

You should be using a proper CSV parser. There is just to many special cases (like Multiline fields, escaped commas) for LINQ to ever realiably deal with. And even if you could, the performance and memory usage would still be bad.

Here is a CSV Parser I often link. Could it be the droid you are looking for? http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • thank you so much Sir! :) but how to implement using this tool I don't have an idea? – PAT Jun 05 '17 at 08:19
  • @PatrickPangilinan: That is far too broad a question and I don't believe that in the two minutes between the answer being made and your comment that you would have had enough time to go and look at that parser and read the documentation. We are not a "write your code" site. We answer specific questions about specific problems. For example if your question was "I've parsed this csv into this data structure but I want to remove the empty entries" then if you've provided your code we could answer. Asking us to write the code for you is just too vague and shows no sign that you've made an effort. – Chris Jun 05 '17 at 08:36
  • @Chris sorry Sir :( I understand thank you! – PAT Jun 05 '17 at 08:39