-1

I'm trying to read an Excel CSV file into a list. I'm able to read in the file and split the fields with no problem. When I loop through the split data and write it to the console it properly displays like this:

003 2 123456789 10 240 ME24-198   100 3/1/2021 3/31/2021
003 2 123456799 11 240 ME24-199   60 3/1/2021 3/31/2021
003 2 123676789 12 240 ME24-153   240 3/1/2021 3/31/2021
003 2 483456789 13 240 ME24-100   500 3/1/2021 3/31/2021
003 2 602456789 14 240 ME24-207   375 3/1/2021 3/31/2021

But when I try to put the data into list zdList my data prints like this:

003
2
123456789
10
240
ME24-198


100
3/1/2021
3/31/2021

Here's what my code looks like:

public partial class Form1 : Form
{
    private List<ZDispatchm> zdList;
    private bool errExit;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void Refresh_Zdispatchm()
    {
       var reader = new StreamReader(File.OpenRead(IqmsDispatchFile));
        while (!reader.EndOfStream)
        {
           var line = reader.ReadLine();
           string[] zdList = line.Split(',');

           foreach (string item in zdList)
           {
             Console.WriteLine(item);
           }
        }
    }
}

I've search this forum and tried numerous suggestions, including these:

How to split a string into substrings and store into a List - c#

read CSV file into a List of strings and then loop to identify an item

How to read csv file and store each cell value into a list (C#) <-- THIS IS THE ONE I'M TRYING TO USE

I've tried this:

Console.WriteLine(item[0], item[1], item[2] . . .etc);

and

Console.WriteLine(item[0].ToString, item[1].ToString);

But they don't work. The compiler emits this error:

CS1503: Cannot convert from 'method group' to 'string'.

In the end, I want the data in the list. I was only printing to the console to confirm that I had access. I'm not sure what I'm doing wrong so I'm wondering if anyone can steer me in the right direction.

NewUser
  • 27
  • 7
  • This line is never used: `private List zdList;` You're creating a `string[]` instead. – Poul Bak Jun 30 '21 at 00:06
  • `.ToString()` is a function. You need to add the `()` behind that. This is really just a basic syntax error. Also, stream readers need to be closed, and, ideally, put in a `using` block so this is handled automatically even when something throws an exception. – Nyerguds Jul 01 '21 at 12:20

2 Answers2

2

Console.WriteLine adds a new line delimiter to the end of your output. What you want is Console.Write then add Console.Write("\n") after your loop.

Like so:

foreach (string item in zdList)
{
  Console.Write(item);
  Console.Write(" ");
}
Console.WriteLine("")
  • @ perpetual-light - I appreciate you trying to help. But I'm a total novice and I'm still not getting it to work. this.zdList.Add(item); gives me error CS1503 Arguement 1: cannot convert from 'string' to 'ScheduleDownload.ZDispatchm. I apologize for not getting it. But I'm learning. – NewUser Jun 30 '21 at 00:32
  • Understandable, you defined it like this: private List zdList. Which means that you can only add object with the type ZDispatchm. So before you add the data to the list you need to assign it to an object of type ZDispatchm. Which you didn't share so I don't know if you would pass your data to the constructor or just assign it to members directly. – perpetual-light Jun 30 '21 at 00:36
  • How bout you add the ZDispatchm class to your question and I'll update my answer with an example. – perpetual-light Jun 30 '21 at 00:40
-2

An easy way to do it is with a package called: FileHelperEngine:

/* Read the files into a List of strings */ 
var sb = new StringBuilder();             
string line;   
var file =  new StreamReader(FilePath);  
while((line = file.ReadLine()) != null) sb.AppendLine(line);

/* Read the files into a List of strings */
var engine = new FileHelperEngine<MyModel>();
MyModel[] records = engine.ReadString(sb.ToString());
CorrieJanse
  • 2,374
  • 1
  • 6
  • 23