How To Read Data From .CSV File In C#

 In this article, I'm going to tell you how to read data from a .csv file in C#.
There are many ways to read the data from .csv file but I found a tool in the Nuget package manager for loading the data of .csv file to a data table which is very much efficient for the work deliverables.
Step 1
Install Lumen CSV Reader package from Nuget Package Manager in Visual Studio
Step 2
Add Namespace "using LumenWorks.Framework.IO.Csv;"
Step 3
The below code is used for loading data into the data table from the file using File.OpenRead() method 
  1. var csvTable = new DataTable();  
  2.                 using (var csvReader = new CsvReader(new StreamReader(System.IO.File.OpenRead(@"D:\CSVFolder\CSVFile.csv")), true))  
  3.                 {  
  4.                     csvTable.Load(csvReader);  
  5.                 }  
Step 4
Now the entire data of the .csv file is loaded to the data table variable
We can access the columns of the .csv file as,
string Column1 = csvTable.Columns[0].ToString(); 
Similarly we can access the rows of the .csv file as ,
string Row1 = csvTable.rows[0][0].ToString();
Sample Image of .csv File
Whatever the first entry you made to .csv file will be considered as column headings and the remaining entries will be considered as rows
Step 5
Now Make the model properties prior to the data
The Below Model Class is for the sample .csv file 
  1. public class SearchParameters  
  2.     {  
  3.         public string FirstName{ getset; }  
  4.         public string LastName{ getset; }  
  5.         public string Email{ getset; }  
  6.     }  
After declaring the properties, we can prepare List using the respective model class name 
  1. List<SearchParameters> searchParameters = new List<SearchParameters>();  
Step 6
After Creating the List, now we can add each row to the list with respect to the property binding 
  1. for (int i = 0; i < csvTable.Rows.Count; i++)  
  2.                 {  
  3.                     searchParameters.Add(new SearchParameters { FirstName= csvTable.Rows[i][0].ToString(), LastName= csvTable.Rows[i][1].ToString(), Email = csvTable.Rows[i][2].ToString() });  
  4.                 }  
Step 7
Now, we had added all the rows of the .csv file to the list with respect to Property Binding.
That's it, we are ready to use those properties.
For Eg: In this Sample Example, I had used those properties to send to Manager Class method 
  1. foreach (var searchparameter in searchParameters)  
  2.                 {  
  3.                     var response = await _CSVManager.GetDataByEntry(searchparameter.FirstName, searchparameter.LastName, searchparameter.Email);  
  4.                   }  
So, in this way, we can load the data of .csv file into data table and can bind the values to properties such that it will be used for future use.
Thanks for reading this article.
All the best for your future endeavours!