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 7968107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:01:30+00:00 2026-06-04T07:01:30+00:00

UPDATE : My original question was invalid because I was misreading the MySql logs.

  • 0

UPDATE: My original question was invalid because I was misreading the MySql logs. Sorry. Please see below, updated.

I am using the LINQ query:

var homework = ctx.Threads.Where(t => t.ClassName == "10L"
                                   && t.EndDate != null
                                   && t.StartDate <= DateTime.Now
                                   && t.EndDate > DateTime.Now)
                          .OrderByDescending(o => o.EndDate)
                          .FirstOrDefault();

This creates the SQL (MySQL 5.5.14):

SELECT
`Project1`.`id`,
`Project1`.`title`,
`Project1`.`startdate`,
`Project1`.`enddate`,
`Project1`.`class`,
`Project1`.`body`,
`Project1`.`threadtype`
FROM (SELECT
     `Extent1`.`id`,
     `Extent1`.`title`,
     `Extent1`.`startdate`,
     `Extent1`.`enddate`,
     `Extent1`.`class`,
     `Extent1`.`body`,
     `Extent1`.`threadtype`
     FROM
     `threads` AS `Extent1`
     WHERE (((`Extent1`.`class` = '10L')
         AND (`Extent1`.`enddate` IS NOT NULL))
         AND (`Extent1`.`startdate` <= (NOW())))
         AND (`Extent1`.`enddate` > (NOW())))
AS `Project1`
ORDER BY `Project1`.`enddate` DESC
LIMIT 1

How does LINQ to EF know to use the NOW() function? Surely I’m just passing it a regular DateTime struct by value?

If I use var now = DateTime.Now; and then use the now variable in the LINQ query, the date is passed as a literal. What’s going on?

  • 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-04T07:01:31+00:00Added an answer on June 4, 2026 at 7:01 am

    LINQ-to-whatever operates by parsing expression trees to convert from .NET code to the appropriate SQL language. This allows the query to process as much as possible database-side instead of client-side. As a result, when you say:

    ... myField <= DateTime.Now
    

    The parser uses the expression reference DateTime.Now, without evaluating it, in order to convert as much as possible of the code to appropriate SQL language. This is what allows LINQ to work efficiently, but as a side effect, everything in the query is interpreted as an expression and attempted to convert to appropriate SQL code. When you store it in a variable instead, as:

    var now = DateTime.Now;
    

    The value is evaluated immediately and stored into now, and that value is used literally in your query instead of the expression.

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

Sidebar

Related Questions

ORIGINAL (see UPDATED QUESTION below) I am designing a new laboratory database that tests
(please read the update section below, I leave the original question too for clarity)
UPDATED - please read further details below original question I have a select form
Original Question mysql-server-6.0.10 I have this problem, I'm using the COMPRESS function to update
Update is below code chunks. Original Question: I have an input field that is
EDIT: See my answer below for the hotfix. ORIGINAL QUESTION: In setting up for
Update So in my Original question I was just frustrated because I was being
Edit: 2020 update -- Please disregard the question below as Google's weather API is
UPDATE: This question is out of date, but left for informational purposes. Original Question
UPDATE : My original question wasn't quite clear. I'm looking for the name of

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.