Link to home
Start Free TrialLog in
Avatar of michael1174
michael1174Flag for United States of America

asked on

linq to sql not between

I have the following where clausing in sql, how do i write the comparable in linq?

where CONVERT(DATE, GETDATE()) not between CheckInDate and  CheckOutDate

I'm trying to query the non active records in my table, which means today's date will not fall in between the check in and check out dates.

Thanks
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

I assumed CheckOutDate and CheckInDate are properties of a data structure (list/array/enumerable etc) the where applies to:

.Where(n=>(DateTime.Now >  n.CheckOutDate) && (DateTime.Now < n.CheckInDate))

Open in new window

Avatar of michael1174

ASKER

Hi thanks I already have that for active records, I need to get NOT active records which is a NOT between.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
one more question, it worked for one of my queries, but my other query, which first i am getting it through a SP:

var query = (get from sp).toList();

   return query.Where(m => (DateTime.Today >= m.StartDate) &&
                        (m.EndDate <= DateTime.Today));

it doesn't seem to be filtering out the records, see any problems?
this query returns all records between StartDate and EndDate (exclusive compare) , is that what u want?

btw, you used DateTime.Today and not DateTime.Now to compare which returns the current date with time  equals to 12:00:00 AM.

for example:

DateTime.Today -> {1/31/2013 12:00:00 AM}
DateTime.Now -> {1/31/2013 14:45:00 PM}
yes, that is what I want but for some reason my query result is wrong.  It doesn't seem like the filter was applied. I'm first calling a sp, and then applying that where.  The method returns a ienumerable and the result is bound to a grid.

thanks for explaination on the date today vs date now.
well the query is simple and straight forward.
debug the StartDate and EndDate and see if it falls within the query range.