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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:31:22+00:00 2026-05-19T04:31:22+00:00

I am currently developing a forum. I am new to LINQ and EF. In

  • 0

I am currently developing a forum. I am new to LINQ and EF. In my forum I have a display that shows a list of topics with the most recent topics first.

The problem is that "most recent" is relative to the topic’s replies. So I don’t want to order the list by the topic’s posted date, rather I want to order the list by the topic’s last reply’s posted date. So that topics with newer replies pop back to the top of the list. This is rather simple if I knew that every topic had at least one reply; I would just do this:

var topicsQuery = from x in board.Topics
                  orderby x.Replies.Last().PostedDate descending
                  select x;

However, in many cases the topic has no replies. In which case I would like to use the topic’s posted date instead. Is there a way within my linq query to order by x.PostedDate in the event that the topic has no replies? I’m getting confused by this.

With the above query, it breaks on topics with no replies because of the x.Replies.Last() which assumes there are replies. LastOrDefault() doesn’t work because I need to access the PostedDate property which also assumes a reply exists.

  • 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-19T04:31:23+00:00Added an answer on May 19, 2026 at 4:31 am
    var topicsQuery = from x in board.Topics
                      let lastActivityDate = x.Replies.Any() 
                             ? x.Replies.Last().PostedDate 
                             : x.PostedDate
                      orderby lastActivityDate descending
                      select x;
    

    Edit:

    To answer your comment, there is no ‘let’ for linq expression syntax explicitly. But, you can achieve the same as follows (using an intermediate select expression):

    var topicsQuery = board.Topics.Select(x => new { 
                                                      Topic = x, 
                                                      LastActivityDate = x.Replies.Any() 
                                                          ? x.Replies.Last().PostedDate 
                                                          : x.PostedDate
                                        })
                                 .OrderByDescending(p => p.LastActivityDate)
                                 .Select(r => r.Topic)
    

    Edit2:

    This can be simplified further as Nicholas was suggesting, we can remove the intermediate select statement as well. Note, this wouldn’t have been possible if not for differed execution.

    var topicsQuery = board.Topics
            .OrderByDescending(x => x.Replies.Any() 
                                          ? x.Replies.Last().PostedDate 
                                          : x.PostedDate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently developing a PHP framework and have ran into my first problem. I need
Currently I am developing a CakePHP that will list various businesses. In the database,
Im currently developing an webapplication that are going to have an Autocomplete search form,
I am developing a web form using Visual Web Developer Currently I have class
I am currently developing an application , that will be published on appstore. I
I'm currently developing a facebook app that needs access to a users photos. It
Currently I'm developing a registration form for new user in Android (native app.) and
I am currently developing a website and i need that the pages loads dynamically
I am currently developing a system that has need for a session time out
I am currently developing a C# Windows Form Application that I intend to let

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.