Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:35:01+00:00 2026-06-06T10:35:01+00:00

I have a simple LINQ query. I would like to only check the DateDisable

  • 0

I have a simple LINQ query. I would like to only check the DateDisable if there is an entry in the database. If the user doesn’t select a date to disable the entry will always show. Can someone please show me how to add a conditional statement within linq

return (from promo in context.Promoes
        where promo.DateEnable <= DateTime.Today
        where promo.DateDisable >= DateTime.Today
        orderby promo.SortOrder
        select promo).ToList();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:35:02+00:00Added an answer on June 6, 2026 at 10:35 am

    Given that DateEnable is a DateTime?, you can do the following:

    // Get today.
    DateTime today = DateTime.Today;
    
    return (
        from promo in context.Promoes
        where 
            promo.DateEnable <= today &&
            (promo.DateDisable == null || promo.DateDisable >= today)
        orderby promo.SortOrder
        select promo
    ).ToList();
    

    Basically, you need to check for null or whether or not the date is greater than today.

    Also you should capture the value outside of the statement, it’s not guaranteed that the the LINQ provider will translate DateTime.Today on the database side correctly.

    However, be warned that because of deferred execution, if you wait a long time to execute the query, today might not give you the value you expect (if the time between declaring the query and executing it rolls past midnight).

    Of course, if your database server is in a different timezone than your application server, then you’ll need to ensure that DateTime.Today is handled by your LINQ provider correctly (so that it’s executed on the server) and use that if you want to compare against time on the DB server. If your provider doesn’t handle translating DateTime.Today correctly, then you’ll have to resort to a stored procedure and call that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you have a simple Linq query like: var result = from record in
I have a simple linq statement that isn't quite returning what I would like.
I have a somewhat simple query in SQL that I would like to convert
I have a simple LINQ query on ADO.NET Entity Framework as follows var result
I have a simple LINQ query that is going against a collection. Dim oList
I have a simple Nhibernate Linq query that is returning more results than expected:
I have a fairly simple Linq query (simplified code): dim x = From Product
I have a simple LINQ-to-Entities query as below: var BillingNumbers = from o in
I have a very simple linq to sql query in C#: int acctNum =
I have a pretty simple Linq to XML query: var q = from c

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.