Monday, April 20, 2020

Read CSV File c#

File Class - It is having methods for creation, copying, deletion, moving and opening of a single file.

Code Snippet:
List<string> masterDataList = File.ReadLines(filepath).ToList();

System.Text.RegularExpressions.Regex CSVParser = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

var columnData = masterDataList.Select(line =>
{
    String[] split = CSVParser.Split(line);
    string column = split[6].Replace("\"", "");
    return column;
}).ToList<string>();




Scenarios

var masterList = File.ReadLines(filePath).ToList();
System.Text.RegularExpressions.Regex CSVParser = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
List<Items> items_MasterData = File.ReadLines(filePath).Select(line =>
{
Items ch = new Items();
System.Text.RegularExpressions.Regex CSVParser = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
String[] split = CSVParser.Split(line);
       ch.col1 = split[0].Replace("\"", "");
       ch.col2 = split[1].Replace("\"", "");
       return ch;
}).ToList<Items>(); 

// CSV files having multiple rows based on col1, so grouping them by col1 data
var group = (from d in items_MasterData
             select new Items
             {
                 col1 = d.col1,
                 col2 = d.col2
             }).GroupBy(n => new { n.col1 })
               .Select(g => g.FirstOrDefault());

//int countCol1Data = items_MasterData.Select(x => x.col1).Distinct().Count();
           
List<Items> col1Data = items_MasterData.GroupBy(n => new { n.col1 }).Select(g => g.FirstOrDefault()).ToList();


No comments:

Post a Comment