Comparing two dates in LINQ to SQL

tech_learner picture tech_learner · May 4, 2011 · Viewed 22.2k times · Source

I have a database with a table named meeting. Meeting dates are stored in this table using the following format: May 2nd 2011 is (for example) formatted as 5/2/2011.

My requirement is to get the meetings between two dates (say, 4/25/2011 and 5/2/2011) and to write a query comparing a date with these two dates. Does it compare like 4/25/2011 < 4/26/2011? How does the comparison take place?

I am using SQL Server 2008 and LINQ to SQL queries.

Answer

MikeM picture MikeM · May 4, 2011

Something like this

DateTime start = new DateTime("4/25/2011");
DateTime end = new DateTime("5/2/2011");
var result = db.Meeting.Where(d => d.MeetingDate >= start 
                 && d.MeetingDate <= end);