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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:38:28+00:00 2026-05-30T17:38:28+00:00

The following LINQ to SQL query is splitting the date into pieces. It seems

  • 0

The following LINQ to SQL query is splitting the date into pieces. It seems odd that a date comparison would use the following generated SQL statement.

var customers = 
(from c in db.customers
 where c.servhists.Any(sh => sh.donedate.Value.Date >= startDate.Date
                && sh.donedate.Value.Date <= endDate.Date
                && sh.donedate.Value.AddDays(triggerDays).Date <= DateTime.Now.Date)

produces the following SQL Query for MS SQL Server 2008

…

WHERE (DATEADD(HOUR, -DATEPART(HOUR, [t3].[donedate]), DATEADD(MINUTE, -DATEPART(MINUTE, [t3].[donedate]), DATEADD(SECOND, -DATEPART(SECOND, [t3].[donedate]), DATEADD(MILLISECOND, -DATEPART(MILLISECOND, [t3].[donedate]), [t3].[donedate])))) >= '6/7/2010') AND (DATEADD(HOUR, -DATEPART(HOUR, [t3].[donedate]), DATEADD(MINUTE, -DATEPART(MINUTE, [t3].[donedate]), DATEADD(SECOND, -DATEPART(SECOND, [t3].[donedate]), DATEADD(MILLISECOND, -DATEPART(MILLISECOND, [t3].[donedate]), [t3].[donedate])))) <= '8/8/2010') AND (DATEADD(HOUR, -DATEPART(HOUR, DATEADD(ms, (CONVERT(BigInt,3 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,3 * 86400000)) / 86400000, [t3].[donedate]))), DATEADD(MINUTE, -DATEPART(MINUTE, DATEADD(ms, (CONVERT(BigInt,3 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,3 * 86400000)) / 86400000, [t3].[donedate]))), DATEADD(SECOND, -DATEPART(SECOND, DATEADD(ms, (CONVERT(BigInt,3 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,3 * 86400000)) / 86400000, [t3].[donedate]))), DATEADD(MILLISECOND, -DATEPART(MILLISECOND, DATEADD(ms, (CONVERT(BigInt,3 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,3 * 86400000)) / 86400000, [t3].[donedate]))), DATEADD(ms, (CONVERT(BigInt,3 * 86400000)) % 86400000, DATEADD(day, (CONVERT(BigInt,3 * 86400000)) / 86400000, [t3].[donedate])))))) <= '10/22/2010')

..
donedate is a nullable column of type DateTime

I can’t imagine this helps performance much. Can anyone suggest a fix/correction that I could do to get rid of this ugly SQL?

  • 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-05-30T17:38:30+00:00Added an answer on May 30, 2026 at 5:38 pm

    The SQL that you saw generated is a result of how the use of the Date property on a column gets rendered into SQL by LINQ. Change your date math to avoid the use of the Date property and other date operations on the column itself. Instead, perform as much of the date math operations as you can on the variable that you are comparing to the column.

    var customers = (from c in db.customers
                     where c.servhists.Any(sh => sh.donedate.Value >= startDate.Date
                                           && sh.donedate.Value < endDate.Date.AddDays(1)
                                           && sh.donedate.Value <= DateTime.Now.AddDays(0 - triggerDays).Date)
    

    This might need some further tweaking so compare results from the one you know works to this before switching. This should be enough to give you the idea.

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

Sidebar

Related Questions

Just wondering how the following sql query would look in linq for Entity Framework...
How would the following sql statement translate to a linq query? select ID, Price,
I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
How do I convert the following query into linq to sql query? select invoice.id,
Can someone help me converting the following SQL Query into LINQ expression? select student.StudentID,
Can someone please help convert the following into LINQ To SQL query? select *
I have the following Linq to SQL query, in which I'm trying to do
I have to following SQL Statement that I want to conver to LINQ Select
I have the following Transact-Sql that I am trying to convert to LINQ ...
Given the following LINQ to SQL query: var test = from i in Imports

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.