Now for this article first we will add a class file and add the below code in it. In this code I have defined few properties.
public class EmployeeModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public DateTime JoiningDate { get; set; }
}
Now we will create a list with the help of above class property, and we will some values. Herer i am having one property named as JoiningDate. This property we will use to select the value between given date. Please refer the below list code to add the data in the list. List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Name="John",Address="Address 1",JoiningDate=DateTime.Now.AddDays(1) },
new EmployeeModel { Id=2,Name="Mate" ,Address="Address 2" ,JoiningDate=DateTime.Now.AddDays(2)},
new EmployeeModel { Id=4,Name="Maru",Address="Address 3" ,JoiningDate=DateTime.Now.AddDays(3) },
new EmployeeModel { Id=5,Name="Krishna",Address="Address 4",JoiningDate=DateTime.Now.AddDays(4) },
new EmployeeModel { Id=8,Name="Pape",Address="Address 5",JoiningDate=DateTime.Now.AddDays(5) },
new EmployeeModel { Id=10,Name="Pranav",Address="Address 6",JoiningDate=DateTime.Now.AddDays(6) }
};
In above list please check the property "JoiningDate". In this I have use DateTime.Now to get the date and added days in the date. This will help us to get the data. Now let's check the complete code. List<EmployeeModel> employees = new List<EmployeeModel>() {
new EmployeeModel { Id=1,Name="John",Address="Address 1",JoiningDate=DateTime.Now.AddDays(1) },
new EmployeeModel { Id=2,Name="Mate" ,Address="Address 2" ,JoiningDate=DateTime.Now.AddDays(2)},
new EmployeeModel { Id=4,Name="Maru",Address="Address 3" ,JoiningDate=DateTime.Now.AddDays(3) },
new EmployeeModel { Id=5,Name="Krishna",Address="Address 4",JoiningDate=DateTime.Now.AddDays(4) },
new EmployeeModel { Id=8,Name="Pape",Address="Address 5",JoiningDate=DateTime.Now.AddDays(5) },
new EmployeeModel { Id=10,Name="Pranav",Address="Address 6",JoiningDate=DateTime.Now.AddDays(6) }
};
//Code to filter data as pere date
var fromDate = DateTime.Now;
var toDate = DateTime.Now.AddDays(3);
var filterData = employees.Where(m => m.JoiningDate >= fromDate && m.JoiningDate <= toDate);
foreach (var item in filterData)
{
Console.WriteLine(string.Format("Id: {0} , Name:{1} , Address:{2} , Joining Date:{3}", item.Id, item.Name, item.Address, item.JoiningDate));
}
Please check the above highlighted code. var fromDate = DateTime.Now;
var toDate = DateTime.Now.AddDays(3);
var filterData = employees.Where(m => m.JoiningDate >= fromDate && m.JoiningDate <= toDate);
Here I have taken two variables in which have defined two variables of type datetime. After that I have uses a range of date with greater then and less then to get the data. Now let's run the code to check the output by putting a breakpoint. Ones it hit the break point we will get the data. of list.
Here you can see the date data. Now let's check the date after applying the filter the data. As per range we will get the three records. Here is the from date and to date.