In this code, You will compare two date without to disregard the time part.
Compare and get date part from datetime in linq
Firstly, create a person class for list then create a list and fill it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Person { public string Name; public string Surname; public DateTime BirthDate; public Person(string name,string surname,DateTime birth) { Name = name; Surname = surname; BirthDate = birth; } } |
Here is the date comparing example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
List<Person> people = new List<Person>(); people.Add(new Person("Emily", "Stone", new DateTime(1989, 4 , 25))); people.Add(new Person("Rupert", "Stone", new DateTime(1989, 4, 15))); people.Add(new Person("Lawrence", "Maaeh", new DateTime(1994, 1, 16))); people.Add(new Person("Kadiveti", "Grint", new DateTime(2001, 11, 09))); people.Add(new Person("Edward", "Lawrence", new DateTime(2000, 01, 01,02,14,10))); people.Add(new Person("Lloyd", "Grint", new DateTime(2004, 4, 25))); DateTime start = new DateTime(1990,01,01);//Comparing date start DateTime end = new DateTime(2000, 01, 01);//Comparing date end //Compare with Linq Query var result = from person in people where person.BirthDate.Date >= start.Date && person.BirthDate.Date <= end select person; foreach (var item in result) { Console.WriteLine("{0} {1} Birth Date:{2}",item.Name,item.Surname,item.BirthDate); } Console.ReadLine(); |