Solved

linq to sql not between

Posted on 2013-01-31
7
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

728 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