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

  • Home
  • SEARCH
  • 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 6936995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:18:23+00:00 2026-05-27T12:18:23+00:00

Background: Entity Framework 4, with SQL Server 2008 Problem: I have a table Order

  • 0

Background:

Entity Framework 4, with SQL Server 2008

Problem:

I have a table Order. Each row has a column Timestamp.

The user can choose some time in past and I need to get the Order closest to the specified time, but that had occurred before the specified time. In other words, the last order before the specified time.

For example, if I have orders

2008-01-12
2009-04-17
2009-09-24
2010-11-02
2010-12-01
2011-05-16

and choose a date 2010-07-22, I should get the 2009-09-24 order, because that’s the last order before the specified date.

var query = (from oData in db.OrderDatas
            where oData.Timestamp <= userTime
            orderby oData.Timestamp ascending
            select oData).Last();

This is closest to what I am trying. However, I am not sure how exactly does the Last operator work when translated to SQL, if it’s translated at all.

Question:

Will this query fetch all data (earlier than userTime) and then take the last element, or will it be translated so that only one element will be returned from the database? My table can hold very large number of rows (100000+) so performance is an issue here.

Also, how would one retrieve the closest time in the database (not necessarily the earlier time)? In the example of 2010-07-22, one would get 2010-11-02, because it is closer to the date specified than 2009-09-24.

  • 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-27T12:18:25+00:00Added an answer on May 27, 2026 at 12:18 pm

    In general, if you’re concerned about how LINQ behaves, you should check what does happen with the SQL. If you haven’t worked out how to see how your LINQ queries are turned into SQL, that should be the very next thing you do.

    As you noted in your comment, Last() isn’t supported by LINQ to SQL so the same may be true for EF. Fortunately, it’s easy to use First() instead:

    var query = (from oData in db.OrderDatas
                 where oData.Timestamp <= userTime
                 orderby oData.Timestamp descending
                 select oData).First();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: For various reasons I'm creating a translation table in my sql server 2008
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
Background I have been now using Entity Framework + Linq for about year and
I ultimately have two areas with a few questions each about Entity Framework, but
Background: Silverlight 4 - RIA - Entity Framework 4 Description: I have some deletion
Background WCF Stack, Data Access Implemented in Entity Framework, Simple ASP.NET Front End This
In Entity Framework (VS2010) how do I create an association to/from the same table?
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I have a few tables that reference the same table. For example: Person has
Some background: We have some entity classes need to be serialized, so we implement

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.