Tuesday, August 12, 2014

Comparing Datetime in Linq expressions

I’ve seen it lot many times that people face difficulties when they are to compare date or time in linq expressions. Well, this would be a quick post with some simple techniques to compare DateTime objects while using clauses in Linq. I’ll use lambda expressions for Linq in this example, but the same thing is applicable in normal Linq expressions too.

So, consider this example for instance. Suppose you have a collection of people you have to meet at a particular date. This is what our class would be:

image

Now we have a List of items of this class as:

image

Let’s fill the list with some values. My expected values actually.

image

Now, let’s write a linq expression to figure out what are the meetings that I have today. There are two ways we can do that. First one is the most legitimate way by simple comparison

image 

another one is a tweak. The legitimate way fails in some cases like when you have different DateTime formats in the DB and of the system.

image

Apparently, both of the queries would yield the same result. Hope this post would be useful.

Share this post

1 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top