I am using the following C# code to parse a csv file, after which the results will be exported to a SQL Server database.
What I am trying to do with the if
statement is say "if the value in column 1 is 1, parse that row one way, if the value in column 1 is 2, parse that row another way..." what I then need to add, where I have the comment in the code below, is to say "otherwise, just skip that row of the csv file."
public List<Entity> ParseCsvFile(List<string> entries, string urlFile)
{
entries.RemoveAt(entries.Count - 1);
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity CsvFile = new Entity();
string[] lineParts = line.Split(',');
if (lineParts[1] == "1")
{
CsvFile.Identifier = $"{lineParts[2]}";
CsvFile.SourceId = $"{lineParts[3]}";
CsvFile.Name = $"{lineParts[5]} {lineParts[6]} {lineParts[7]} {lineParts[8]} " +
$"{lineParts[9]} {lineParts[10]}";
entities.Add(CsvFile);
}
else if (lineParts[1] == "2")
{
CsvFile.Identifier = $"{lineParts[11]}";
CsvFile.SourceId = $"{lineParts[12]}";
CsvFile.Name = $"{lineParts[13]} {lineParts[14]} {lineParts[15]};
entities.Add(CsvFile);
}
//Need to put code here that says "otherwise, skip this line of the CSV file."
}
return entities;
}