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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:18:40+00:00 2026-05-26T21:18:40+00:00

I am using Entity Framework 4.1 with POCOs and DbContext, no proxies no lazy

  • 0

I am using Entity Framework 4.1 with POCOs and DbContext, no proxies no lazy loading.

I am very new to Entity Framework and LINQ, but so far very impressed with how simple it is to work with it.
I have some basic knowledge of SQL and have built the database first, and then created the model.

My problem involves 3 tables (I only left in what is relevant):

CREATE TABLE [dbo].[Users](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL
)

CREATE TABLE [dbo].[UserFriends](
    [UserId] [int] NOT NULL,
    [FriendId] [int] NOT NULL
)

CREATE TABLE [dbo].[UserStatuses](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [UserId] [int] NOT NULL,
    [Message] [nvarchar](max) NOT NULL
)

The PK are Usres.Id, UserStatuses.Id, UserFriends.UserId + UserFriends.FriendId.
UserId is also a FK to Users.Id .

Each user can have many statuses and many friends

I hope the database structure is clear.

What I am trying to do is get a list of users who are my friends and their most recent status.

In SQL, it will look like:

SELECT * FROM Users U 
OUTER APPLY 
(
    SELECT TOP 1 *
    FROM UserStatuses
    WHERE UserId = U.Id
    ORDER BY Id DESC
) S
WHERE U.Id IN (SELECT FriendId FROM UserFriends WHERE UserId = 5)

This will get all the friends for ‘5’ and their latest status message.
I think this is the most efficient way (inner join on friends and users and outer join on the status messages), but this is not the question.

I would like to do that using entity framework.
I found out how to get a list of my friends:

var friends = db.UserFriends.Where(f => f.FriendId.Equals(userId)).Select(f => f.User);

But if I will add the .Include(u => u.UserStatuses) to get the statuses, I will get all of them, I would like to return just the most recent one.

The only thing I managed to do in order to get it to work is:

var friends = db.UserFriends.Where(f => f.FriendId.Equals(userId)).Select(f => f.User);

foreach (Model.User friend in friends)
{
    db.Entry(friend).Collection(f => f.UserStatuses).Query().OrderByDescending(s => s.Id).Take(1).Load();
}

But what happens now is that for each friend I generate another SQL query to the database, this seems like (very) bad practice.

How can I make it happen in a single query?
Would appreciate any advice on how to load the latest inserted related entity.

Thanks,

  • 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-26T21:18:41+00:00Added an answer on May 26, 2026 at 9:18 pm

    Only way to get this in a single database query is a projection:

    var friendsWithLastStatus = db.UserFriends
        .Where(f => f.FriendId.Equals(userId))
        .Select(f => new 
        {
            User = f.User,
            LastStatus = f.User.UserStatuses
                               .OrderByDescending(s => s.Id).FirstOrDefault()
        })
        .ToList();
    

    This gives you a list a anonymous type objects where each entry has a User property and the LastStatus property with the latest status of that user.

    You can project into a named type if you prefer:

    public class UserWithLastStatus
    {
        public User User { get; set; }
        public UserStatus LastStatus { get; set; }
    }
    

    And then replace in the query above new ... by new UserWithLastStatus ... to get a List<UserWithLastStatus> as the result type of the query.

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

Sidebar

Related Questions

The goal: I'm trying to use the new Entity Framework 4.1 DbContext API (using
I have a problem with the following Linq query using Entity Framework: from o
I am using the entity framework 4 with edmx files and POCOs within an
I am using ASP.NET MVC 3 with Entity Framework 4 using POCOs and want
I'm using Entity Framework 4.1 (database first, e.g old school), and i'm using POCOs
I'm using Entity Framework Code-First with POCOs for my database interaction. I have a
What does one loose by creating POCO using T4 templates in entity framework 4.0?
Using Entity Framework, I suddenly get this strange error after publishing my asp.net mvc
I'm using Entity Framework O/R mapper from Microsoft and using entity classes (generated classes
I am using Entity Framework with my website. To improve performance, I have started

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.