0

I want to search two columns from CSV file like the data of Flying From and Flying To and then display the result in console. I have tried searching and everything is really hard for me to understand. Appreciate your support.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mark Riz
  • 1
  • 1
  • 1

1 Answers1

0

You could use this code from the link

using System.IO;

static void Main(string[] args)
{
    var reader = new StreamReader(File.OpenRead(@"C:\test.csv"));
    List<string> listFlyingFrom = new List<string>();
    List<string> listFlyingTo = new List<string>();
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(';');

        listFlyingFrom.Add(values[0]);
        listFlyingTo.Add(values[1]);
    }
}

or you create a new object:

public class Flying {
    public string FlyingFrom { get; private set; }
    public string FlyingTo { get; private set; }

    public Flying(string from, string to) {
        FlyingFrom = from;
        FlyingTo = to;
    }
}

using System.IO;

static void Main(string[] args)
{
    var reader = new StreamReader(File.OpenRead("test.csv"));
        List<Flying> flying = new List<Flying>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');

            flying.Add(new Flying(values[0], values[1]));
        }

        string userSelection = "fromA";
        Flying result = flying.Find(f => f.FlyingFrom.Equals(userSelection));
        Console.WriteLine(result.FlyingFrom + ": " + result.FlyingTo);
}
Community
  • 1
  • 1
wake-0
  • 3,918
  • 5
  • 28
  • 45