Remove or Replace all Null Value form List Using LINQ C#.Net

How to remove record have null value from list and replace the null value with NA in list using LINQ in C#.Net by Lambda expression or SQL like query.
In today's article I will show you how you can remove all the null value or replace all the null value of a column with 'NA' or any other value using LINQ in C#.Net. This article examples you can use in your .NET core application also. 

Here we will demonstrate the example in a .net core console application. So, let's create a new console application and check the code example. After creating the console appellation inc#.net we will add a class file and add the below defined property.

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int DepartmentId { get; set; }
        public string PhoneNo { get; set; }
    }
In above code i have defined some property in the class. Now let's add some values in the by making a List. 
List<Employee> employee = new List<Employee>();
employee.Add(new Employee { Id = 1, Name = "Rakesh", DepartmentId = 2 });
employee.Add(new Employee { Id = 2, Name = "Dhiru", DepartmentId = 3, PhoneNo = "234234234" });
employee.Add(new Employee { Id = 3, Name = "Suraj", DepartmentId = 5, PhoneNo = "433233423" });
employee.Add(new Employee { Id = 4, Name = "Rishant", DepartmentId = 1 });
Here in above list, I have into added value of PhomeNo for two of the record.  So, I have broken the code into two parts first we will show how we can remove the records from list which are having null value for column PhoneNo and in second example we will show how we can add "NA" for those records where phone no is null. 

How to Remove Records or Row Having Null?

Please check the below code where I have applied a null check condition using LINQ to remove the record which are having null value.

Lambda Expression Query:

var data = employee.Where(m => m.PhoneNo != null).ToList();
foreach (var item in data)
{
    Console.WriteLine(string.Format("Name:{0}, Employee Code :{1}, PhoneNo:{2}", item.Name, item.EmployeeCode, item.PhoneNo));
}

SQL Like Query:

var data = from e in employee
         where e.PhoneNo != null
         select e;
foreach (var item in data)
{
    Console.WriteLine(string.Format("Name:{0}, Employee Code :{1}, PhoneNo:{2}", item.Name, item.EmployeeCode, item.PhoneNo));
}
In above query you can i have checked null value for the for the column  PhoneNo. Now let's run the code and check the output by putting a break point. 

Remove record having null value using linq in c#.net
In above snippets you can see that we are getting two records which are not having null value. Now let's press F5 to check the output.

How to Remove Records or Row Having Null?

How to Replace Record with NA w Having Null?

Now we will check how we can replace the null value with NA present in a column using LINQ in c#.net. Please check the below code. 
List<Employee> employee = new List<Employee>();
employee.Add(new Employee { Id = 1, Name = "Rakesh", EmployeeCode = 200 });
employee.Add(new Employee { Id = 2, Name = "Dhiru", EmployeeCode = 300, PhoneNo = "234234234" });
employee.Add(new Employee { Id = 3, Name = "Suraj", EmployeeCode = 500, PhoneNo = "433233423" });
employee.Add(new Employee { Id = 4, Name = "Rishant", EmployeeCode = 100 });

var data = from e in employee
         select new { e.Id, e.Name, e.EmployeeCode, PhoneNo = e.PhoneNo == null ? "NA" : e.PhoneNo };
foreach (var item in data)
{
    Console.WriteLine(string.Format("Name:{0}, Employee Code :{1}, PhoneNo:{2}", item.Name, item.EmployeeCode, item.PhoneNo));
}
In above code check the query written with from to get the records. Here I have applied null check while selecting the value of the for column which is having null value. If the value is null, it has been replaced with "NA".
var data = from e in employee
         select new { e.Id, e.Name, e.EmployeeCode, PhoneNo = e.PhoneNo == null ? "NA" : e.PhoneNo };
Please check the highlighted part of the code. Here we are replacing the null value with "NA". Now lets put a break point and check the output.

Replace Null Value With Na From List in Linq Using C#.net
Here you can see the record where we are having PhoneNo null have been replaced with "NA". Now lets press F5 and check the output.

Example Replace Null Value With Na From List in Linq Using C#.net

Post a Comment