Time and Date
Create a new class and named it GameResult. First, we have to create a constructor to this class.
class GameResult
{
public DateTime GameDate { get; set; }
}
Try in your c# interactive window, type:
> DateTime.Now;
> DateTime.Now.DayOfWeek;
Back to our Main class. We don't want the first line to be read. So we could manipulate it with call the first line first with reader.ReadLine()
.
public static List<string[]> ReadFootballResults(string fileName)
{
var soccerResults = new List<string[]>();
using (var reader = new StreamReader(fileName))
{
var line = "";
reader.ReadLine();
while ((line = reader.ReadLine()) != null)
{
string[] values = line.Split(',');
soccerResults.Add(values);
}
}
return soccerResults;
}
Then we need to call our GameResult class inside a while.
var GameResult = new GameResult();
Define a new DateTime variable named gameDate.
DateTime gameDate;
Then we try to parse the date and time from csv to gameDate variable.
while ((line = reader.ReadLine()) != null)
{
var GameResult = new GameResult();
DateTime gameDate;
string[] values = line.Split(',');
if (DateTime.TryParse(values[0], out gameDate))
GameResult.GameDate = gameDate;
soccerResults.Add(values);
}
Our ReadFootbalResults function should look like this.
public static List<string[]> ReadFootballResults(string fileName)
{
var soccerResults = new List<string[]>();
using (var reader = new StreamReader(fileName))
{
var line = "";
reader.ReadLine();
while ((line = reader.ReadLine()) != null)
{
var GameResult = new GameResult();
DateTime gameDate;
string[] values = line.Split(',');
if (DateTime.TryParse(values[0], out gameDate))
GameResult.GameDate = gameDate;
soccerResults.Add(values);
}
}
return soccerResults;
}