How to Compare Two CSV Files in Asp.Net Core MVC Using C#.Net, LINQ

How to compare two csv files in asp.net core mvc using in c#.net. Read the CSV file from wwwroot folder and display data difference in table format.
In today's article I will show you how you can compare tow to CSF file data in asp.net core mvc using LINQ in c#.net. Here first I will read the two of the CSV files in two different list and then use LINQ to compare the list values and get the difference between to CSV files data and display the value on view in tabular format.

Now for this article first we will create a new asp.net core 8 mvc application with c#.net and add two CSV files. Here are the two data.

CSV File Data
Please check the two above CSV files. In these two files record 2 is same and rest is different. So, we will copy the files in wwwroot folder. Now let's add the files in wwwroot folder and we will read files from wwwroot folder

CSV file in wwwroot folder

After creating the asp.net core mvc application, we will add a model class file names as Employee in the model folder and add the below code it.
public class Employee
{
    public int EmpId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public List<Employee> Employees { get; set; }
}
In this mode class file i have defined the few propertied for employee. This model class property we will user to display the filtered employee detail in the view. Now let's add a controller class file and add a controller class file and create a HttpGet mentod in your controller file. Here in this article, we will user compare the CSV files and map the value to model class file to display it on the view.
[HttpGet]
public IActionResult Index()
{
    string parentPath = @"wwwroot\CSV\";
    Employee employee = new Employee();
    employee.Employees = new List<Employee>();
    string file1Path = Path.Combine(Directory.GetCurrentDirectory(), parentPath, "UserList_1.csv");
    string file2Path = Path.Combine(Directory.GetCurrentDirectory(), parentPath, "UserList_2.csv");
    var dataList1 = System.IO.File.ReadAllLines(file1Path);
    var dataList2 = System.IO.File.ReadAllLines(file2Path);
    var dataDifferrence = dataList1.Except(dataList2).Select(m => m.Split(','));
    foreach (var data in dataDifferrence)
    {
        employee.Employees.Add(new Employee { EmpId = Convert.ToInt32(data[0]), Name = data[1], Address  = data[2] });
    }
    return View(employee);
}
In above code i have taken a string type of variable to define the folder path where we have stored the CSV file in the wwwroot folder in your asp.net core mvc application. After declaring the path of the CVS file folder i have created the object of the Employee model class file. 

Now let's prepare the path for both of the CSV file by using Path.Combine and then pass the Directory.GetCurrentDirectory() to get the current directory path. Now pass the wwwroot folder path and last but not the leaset passing the name of the CSV file using C#.Net. Here actually we are combining or concatenating the path to get a path of the CSV file. 
string file1Path = Path.Combine(Directory.GetCurrentDirectory(), parentPath, "UserList_1.csv");
string file2Path = Path.Combine(Directory.GetCurrentDirectory(), parentPath, "UserList_2.csv");
After preparing the path of the CSV file we will read the content of both the CSV file by using System.IO.File.ReadAllLine. Please check the below code. 
var dataList1 = System.IO.File.ReadAllLines(file1Path);
var dataList2 = System.IO.File.ReadAllLines(file2Path);
When we read the CSV file we will get the read data in below format.

Read CSV file Data in C#.Net
In above code we are getting all the data in the string format. After reading the CSV file data by using System.IO.File.ReadAllLine. We will compare the CSV file data list using Except function. This function will compare and remove the record from which is available in the second. Now lets run the code and check the output. 
In above screen shot we are getting each row as string, currently I am not breaking the record into column. Now check the next piece of code.
dataList1.Except(dataList2).Select(m => m.Split(','));
In above piece of code, I have selected the row and use lambada expression and split methos to split the string rows into columns. After that i have used foreacch loop push the values to the Employee list.
 
Employee List from CSV Data

Now let's create the view and add the below code in it. In this we will display the filtered record. 
@model Employee
@{
    ViewData["Title"] = "Home Page";
}
<h1>Difference Between Two CSV List</h1>
<br />
<table class="table">
    <thead>
    <th>Emp. Id</th>
    <th>Name</th>
    <th>Address</th>
    </thead>
    <tbody>
        @foreach (var item in Model.Employees)
        {
            <tr>
                <td>@item.EmpId</td>
                <td>@item.Name</td>
                <td>@item.Address</td>
            </tr>
        }
    </tbody>
</table>
In above code I have added the reference of the model class. Now I have taken an HTML table to display the Employee list in tabular or grid form. Now let's check the output.

How to Compare Two CSV Files in Asp.Net Core MVC Using C#.Net, LINQ

Post a Comment