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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:38:20+00:00 2026-05-25T18:38:20+00:00

Well, I think trying to understand someone elses SQL can be trying enough but

  • 0

Well, I think trying to understand someone elses SQL can be trying enough but trying to convert said SQL to Entity Framework is another level of pain…

I have this SQL:

SELECT 
    MAX(IntegerColumn) - MIN(IntegerColumn) 
    / 
    DateDiff(day, MIN(DateColumn) , MAX(DateColumn)) 
    * 
    DateDiff(day, MAX(DateColumn) , @TargetDate) 
    + 
    MAX(IntegerColumn) 
    AS Calculation      
FROM MyTable 
WHERE TargetId = @TargetId

Forgetting operator precedence, so far, I have come up with the following LINQ to Entities, which I am regarding with extreme suspicion:

var calculation = 
    (from f in this.ObjectContext.MyTable 
        where f.TargetId == targetId
        group f by f.IntegerColumn into o
        let maxIntegerColumn = o.Max(x => x.IntegerColumn)
        let minIntegerColumn = o.Min(x => x.IntegerColumn)
        let maxDate = (from t in o select t.DateColumn).Max()
        let minDate = (from t in o select t.DateColumn).Min()
        select new
        {
            Result = (maxIntegerColumn - minIntegerColumn) /
            ((SqlFunctions.DateDiff("day", minDate, maxDate) *
            SqlFunctions.DateDiff("day", maxDate, targetDate)) + maxIntegerColumn)
        }).FirstOrDefault();

if (calculation != null)
{
    if (calculation.Result != null)
    {
        return calculation.Result.ToString();
    }
}

That just looks wrong, wrong, wrong and is the result of blindly following too many blog posts.

Is this in the right direction? How can I simplify 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-05-25T18:38:21+00:00Added an answer on May 25, 2026 at 6:38 pm

    If it works, go with it. You can reformat and refactor your LINQ to make it look prettier but it appears as though it’s doing the right thing.

    In this case, I’d probably write a stored procedure – you’re performing awkward calculations that need to be done in the database, so just write them in the database and then execute them from your code.

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

Sidebar

Related Questions

I'm currently trying to understand some assembler code well enough to reconstruct the C
In general I think I can convey most programming related concepts quite well. Yet,
You could very well leave out as a technical lead, but I think it
I think I've got the format of Recursive CTEs down well enough to write
I think I understand references and pointers pretty well. Here is what I (think
Well, I'm trying to follow the tutorial from http://rapidandroid.org/wiki/Graphing . But I found a
I am trying to convert my model-first project to code-first, as I can see
I'm using the Stack Exchange Data Explorer to learn SQL, but I think the
I think database operations isn't explained very well, on guide . I couldn't understand
I think I have understood how MeasureOverrride works, but I am trying to use

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.