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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:02+00:00 2026-05-27T07:20:02+00:00

I have a database with these two tables: Customer and CustomerStatus . CustomerStatus is

  • 0

I have a database with these two tables: Customer and CustomerStatus. CustomerStatus is a so called readonly table. All changes to a customer’s status result in an INSERT into that table. The current customer status is locatable via CustomerStatus.StatusTimestamp.

Now I’m looking for a LINQ to entities query that loads all Customers together with their current status. Something like

.Include("CustomerStatus.OrderByDescending(StatusTimestamp).FirstOrDefault()").

I haven’t found a proper way with with EF 4.0.

My current workaround is very ugly and uses 2 steps:

step 1: LINQ query that loads all status (in the business/data layer):


var r = from c in db.Customers.Include("CustomerStatus")
select c;

step 2: take the suitable status from each customer in the GUI (in a loop in a MVC3 view):


c.CustomerStatus.OrderByDescending(i => i.StatusTimestamp).FirstOrDefault()

Any idea how this can be done directly in one step?

  • 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-27T07:20:03+00:00Added an answer on May 27, 2026 at 7:20 am

    EF is more powerful than you give it credit for. One of the things it can do is project to complex types within an IQueryable (ie, in one database hit). This should work:

    var data = 
        from c in db.Customers
        select new { 
            Customer = c, 
            LastStatus = c.CustomerStatus  // assuming navigation property set up right
                          .OrderByDescending(s => s.StatusTimestamp)
                          .FirstOrDefault()
        };
    

    Now data is an IQueryable<anonymous_type>, and each item has a Customer property giving the customer, and a LastStatus giving the latest-timestamped status, or null if there aren’t any status records for the customer.

    You can use a named type here too, it’s just quicker for me to type it this way :).

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

Sidebar

Related Questions

Say I have a database called Poll and have these two table structures. poll
We have two tables in a SQL Server 2005 database, A and B.There is
I have a database table (named Topics) which includes these fields : topicId name
i have a stack of messages in database table. i want to send these
I have two tables in a SQL Server 2008 database in my company. The
I have database with two tables: Customers (Id PK, LastName) and Orders (Id PK,
I have two tables in the clients mssql database. The first is a job
Let's say my SQL database contains a table called 'Customer'. This Customer has an
I have a relational database with about 100 tables. Each table has unique, numerical,
I have two tables, jos_eimcart_customers_addresses and jos_eimcart_customers. I want to pull all records from

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.