I'm writing the following method in C# to parse a CSV file and write values to a SQL Server database.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
public List<Entity> ParseEntityExclusionFile(List<string> entries, string urlFile)
{
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity exclusionEntity = new Entity();
string[] lineParts = line.Split(',').Select(p => p.Trim('\"')).ToArray();
exclusionEntity.Id = 1000;
exclusionEntity.Names = lineParts[3];
exclusionEntity.Identifier = $"{lineParts[25]}" + $" | " + $"Classification: " + ${lineParts[0]}";
entities.Add(exclusionEntity);
}
return entities;
}
The data in some columns of the csv file are comma-separated values inside a set of parentheses, meant to represent one value for that column. So for any values like that, I need to capture it as one value to go into one field in the database. How would I adjust/add-to the line of code string[] lineParts = line.Split(',').Select(p => p.Trim('\"')).ToArray();
to instruct the application that if it encounters a column with open parenthesis, capture all the data after the open parenthesis, including the commas, until the close parenthesis, all as one value?
EDIT: it seems the Select(p => p.Trim('\"')).ToArray();
part of the above line of code is confusing some folks - don't worry about that part - I just need to know how I would go about adding 'exception' code to create a condition where Split(',')
is ignored if the commas happen to be in between a set of parentheses. One field in the csv file looks like this (1,2,3,4)
- currently the code parses it as four fields, whereas I need that parsed as one field like 1,2,3,4
OR (1,2,3,4)
it actually doesn't matter whether the resulting fields contain the parentheses or not.
EDIT 2: I appreciate the suggestions of using a .NET CSV library - however, everything is working perfectly in this project outside of this one field in the csv file containing a set of parentheses with comma-separated values inside - I feel as though it's a bit overkill to install and configure an entire library, including having to set up new models and properties, just for this one column of data.