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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:27:25+00:00 2026-05-12T08:27:25+00:00

Having two classes like Blog and Post, in Entity Framework (and LINQ-to-Entities), how can

  • 0

Having two classes like Blog and Post, in Entity Framework (and LINQ-to-Entities), how can you get the blogs with the posts sorted by date. I was getting the blogs with the posts this way:

from blog in db.BlogSet.Include("Posts") select blog

and now I’m forced to do this:

public class BlogAndPosts {
    public Blog Blog { get; set; }
    public IEnumerable<Post> Posts { get; set; }
}

from blog in db.BlogSet
select new BlogAndPosts () {
    Blog = blog,
    Posts = blog.Posts.OrderByDescending(p => p.PublicationTime)
}

which is very convoluted and ugly. The reason why I’m creating a BlogPosts class is that now, since I have to pass two variables, Blog and Posts, to MVC, I need a view model.

I’m even tempted to try this hack:

from blog in db.BlogSet
select new Blog(blog) {
    Posts = blog.Posts.OrderByDescending(p => p.PublicationTime)
}

but what’s the correct way to do it? Is Entity Framework not the way to go with MVC?

  • 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-12T08:27:26+00:00Added an answer on May 12, 2026 at 8:27 am

    I generally create a presentation model type wholly unaware of the Entity Framework and project into that. So I would do something like:

    public class PostPresentation {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public DateTime PostTime { get; set; }
        public string Body { get; set; }
    }
    
    public class BlogHomePresentation {
        public string BlogName { get; set; }
        public IEnumerable<Post> RecentPosts { get; set; }
    }
    
    from blog in db.BlogSet
    select new BlogHomePresentation 
    {
        BlogName = blog.name,
        RecentPosts = (from p in blog.Posts
                       order by p.PublicationTime desc
                       select new PostPresentation 
                       {
                           Id = p.Id,
                           Title = p.Title,
                           PostTime = p.PublicationTime,
                           Body = p.Body
                       }).Take(10)
    }
    

    Does this seem like a lot of work? Consider the advantages:

    1. Your presentation is entirely ignorant of your persistence. Not “ignorant as in having to make all of the properties public virtual,” but entirely ignorant.
    2. It is now possible to design the presentation before designing the database schema. You can get your client’s approval without doing so much work up front.
    3. The presentation model can be designed to suit the needs of the page. You don’t need to worry about eager loading or lazy loading; you just write the model to fit the page. If you need to change either the page or the entity model, you can do either one without affecting the other.
    4. Model binding is easier with simple types. You will not need a custom model binder with this design.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 280k
  • Answers 280k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One issue with the MemoryStream is the internal buffer doubles… May 13, 2026 at 3:33 pm
  • Editorial Team
    Editorial Team added an answer Lucene use locking internally to maintain index consistency, so you… May 13, 2026 at 3:33 pm
  • Editorial Team
    Editorial Team added an answer What I think you want to do is build your… May 13, 2026 at 3:33 pm

Related Questions

When programming in PHP I always try to create meaningful 'models' (classes) that correspond
I'm mapping a legacy database with nhibernate and having some problems with mapping a
I've been reading that the static methods of the File Class are better used
I have two classes one of which inherits from the other. Im trying to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.