Solved

linq to sql not between

Posted on 2013-01-31
7
476 Views
Last Modified: 2013-01-31
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
0
Comment
Question by:michael1174
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38838930
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

0
 
LVL 2

Author Comment

by:michael1174
ID: 38838945
Hi thanks I already have that for active records, I need to get NOT active records which is a NOT between.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38838959
sorry:

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

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 2

Author Comment

by:michael1174
ID: 38839034
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?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38839231
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}
0
 
LVL 2

Author Comment

by:michael1174
ID: 38839307
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.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38839313
well the query is simple and straight forward.
debug the StartDate and EndDate and see if it falls within the query range.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 40
SQL Help 27 45
Update a text value in another table 10 40
Separate 2 comma delimited columns into separate rows 2 32
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question