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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:20:54+00:00 2026-06-05T20:20:54+00:00

I keep coming across the same type of difficulty in several places in my

  • 0

I keep coming across the same type of difficulty in several places in my current project.

The simplest instance is as follows:

I have two related entities, Request and RequestAction.
Each RequestAction has a status and a timestamp and points back to a Request.

I need to query a collection of Requests based on the status of the most recent related RequestAction.

Example: Get all Requests where the status of the most recent RequestAction is “Open”.

Denormalizing the database to make the most recent status a property of the Request entity is not an option.

I need need the same type of filtering in many other places in my application; I keep track of many versions of items for history and auditing, but I usually only want to look at the most recent version of each item.

I can think of two solutions, but neither is particularly appealing.

1) I can write the SQL manually. The raw SQL answer to this problem involves joining the table to itself with an outer join on the timestamp of one instance of the table being larger than the other, then looking for the records where the second table is null, indicating that no larger timestamp could be found. This is how I handeled the scenario in the past. It is efficient in the database as it uses indexes, unlike a subquery with a max in it. However, my development team is being strongly pushed to use entity going forward to talk to the database, so this solution is being strongly discouraged.

2) I can use entity to load all the values into memory and loop through manually in my code to find the values I am looking for. The code would look something like this:

var openRequests = new List<Request>();
var allRequests = context.Requests;
foreach (var request in allRequests)
{
    var recentAction = request.RequestActions
        .OrderByDescending(c => c.ActionTimestamp)
        .First();
    if (recentAction.Status == "Open")
    {
        openRequests.Add(request);
    }
}

While this would provide me with the results I want, it is horribly inefficint and a huge waste of resources and execution time. The database I am querying against is very large and iterating through each record is really not feasable.

Is there an efficient way to do this using entity? I find it hard to imagine that I am the only one who has ever needed this type of functionality.

  • 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-05T20:20:55+00:00Added an answer on June 5, 2026 at 8:20 pm

    You can query this with LINQ to Entities in a single DB query:

    var openRequests = context.Requests
        .Where(r => r.RequestActions
            .OrderByDescending(ra => ra.ActionTimestamp)
            .Select(ra => ra.Status)
            .FirstOrDefault() == "Open")
        .ToList();
    

    The generated SQL looks like this:

    SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[SomeProp1] AS [SomeProp1], 
    [Extent1].[SomeProp2] AS [SomeProp2], -- ...etc.
    FROM  [dbo].[Requests] AS [Extent1]
    CROSS APPLY  (SELECT TOP (1) [Project1].[Status] AS [Status]
        FROM ( SELECT 
               [Extent2].[Status] AS [Status], 
               [Extent2].[ActionTimestamp] AS [ActionTimestamp]
               FROM [dbo].[RequestActions] AS [Extent2]
               WHERE [Extent1].[Id] = [Extent2].[RequestId]
        ) AS [Project1]
        ORDER BY [Project1].[ActionTimestamp] DESC ) AS [Limit1]
    WHERE N'Open' = [Limit1].[Status]
    

    I have no idea though if this is a “good” and performant SQL or not.

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

Sidebar

Related Questions

So, one problem pattern that I keep coming across and don't have a good
I keep coming across techniques like the code below where i need to filter
I keep coming across the use of this word and I never understand its
I've been studying objective-c for a few days now. I keep coming across these
I'm learning javascript. Poked around this excellent site to gather intel. Keep coming across
The frequency with which I am coming across the situation where I have to
I've looked through several Google searches now, and continue to keep coming up empty
I am (trying to) learn Objective-C and I keep coming across a phrase like:
Ok so I've recently started doing some reverse engineering, and I keep coming across
I keep coming across this common error then compiling my program. I've already tried

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.