0

I try to add the containing of a csv file to a new list. It's a list of different types of people with caracteristics like the function, a matricule, the last name, the firstname and a sex. So I managed to read the file but I don't really know how to process to add the containing of the file to my list.Here is my code :

  `private static void ReadTest() 
    {
        int count = 0;
        string line;
        Char c = ';';

        StreamReader file= new StreamReader("Listing.csv");

        while ((line= file.ReadLine()) != null)
        {
            String[] substrings = line.Split(c);

            foreach (var substring in substrings)
            {
                Console.WriteLine(substring);
            }

            count++;
        }

        fichier.Close();
        System.Console.WriteLine("Number of lines : {0}.", count);
    }
    static void Main(string[] args)
    {
        List<Personnel> Workers = new List<Personnel>();

    }

'

2
  • If "line" in your loop is a person, with comma separated attributes, you're 99% of the way there. Either parse the attributes into a new Personnel object, and then add that object to a List<Personnel>, or add "line" to a List<string> and return that from the reader. Then you can parse the "line" collection to Personnel in a different method. All you're missing from your code is the List<string> to store the values you are reading from the file. Commented Mar 21, 2018 at 15:52
  • Yeah, seems like all you need to do now after the split is basically Personnel p = new Personnel() and then fill the properties in like, p.LName = substrings[0], p.FName = substrings[1] etc... and finally add p to your list. You just need to map the columns to the correct properties. Commented Mar 21, 2018 at 23:12

2 Answers 2

1

Why don't you use CSVHelper, it will be as simple as the following:

var csv = new CsvReader( textReader );
var records = csv.GetRecords<Personnel>();
//then loop through
foreach( var record in records )
{

}

you just need to install the nuget package:

Install-Package CsvHelper

Check this for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a school project and I have constraints including not to use CsvHelper
1

Replace the foreach loop with something like the following:

var person = new Personnel(); 
person.firstname = substrings[0];
person.lastname = substrings[1]; 
person.function = substrings[2]; 
//continue until all variables assigned. 
Workers.Add(person); 

Also, if the Workers list is not a static list, then make ReadTest return a List and create a list within the function.

Like this:

private static List<Personnel> ReadTest() 
{
    int count = 0;
    string line;
    Char c = ';';

    StreamReader file= new StreamReader("Listing.csv");
    var Workers = new List<Personnel>();

    while ((line= file.ReadLine()) != null)
    {
        String[] substrings = line.Split(c);

        var person = new Personnel(); 
        person.firstname = substrings[0];
        person.lastname = substrings[1]; 
        person.function = substrings[2]; 
        //continue until all variables assigned. 
        Workers.Add(person); 

        count++;
    }

    file.Close();
    System.Console.WriteLine("Number of lines : {0}.", count);
    return Workers; 
}
static void Main(string[] args)
{
    List<Personnel> Workers = ReadTest();

}

2 Comments

Thank you very much, but I have another question. One of the caracteristics to add is an enum type, so how can I make it correspond to the substrings?
Here is an SO on how to find an enum from a string: stackoverflow.com/questions/11508865/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.