Solved

linq to sql not between

Posted on 2013-01-31
7
471 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
backups - Strategies 1 13
shrink datafile Sql server 4 14
Iterate two lists and remove inactive 11 28
Birthdays 3 0
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now