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.
Asked
Active
Viewed 60 times
0
-
Please read [Ask] and also take the [Tour] – Ňɏssa Pøngjǣrdenlarp Jun 04 '16 at 16:46
1 Answers
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);
}
-
I tried this method but had problem like it was not searching two columns but searching only one and when I wanted to display it on the console it only showed same column twice. – Mark Riz Jun 04 '16 at 16:37
-
-
Yes bro. sorry i did not mention it earlier that i want the data to be displayed when user enters the location then the program searches for it and then displays the result. – Mark Riz Jun 04 '16 at 16:52
-
-