LINQ Does Not Support Date Comparison

.net 3.5 Linq queries does not seem to support date comparison. For example following code did not support.

entities.Object.Where(p => p.ToDateTime.Date>=DateTime.Now.Date
&& p.FromDateTime.Date<= DateTime.Now.Date)

But it supported comparison of date time.

entities.Object.Where(p => p.ToDateTime>=DateTime.Now
&& p.FromDateTime<= DateTime.Now)

Finally found a solution to compare the Date only trough linq query

entities.Object.Where(p => p.ToDateTime>=DateTime.Today
&& p.FromDateTime<= DateTime.Today)

Note:In my case the date object stored in the database recorded 00:00 as its time. In C# with the  DateTime.Today it sets the time to 12:00AM which is 00:00 in SQL.

This worked for me. Hope it’s helpful to you as well

Leave a comment