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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:44:56+00:00 2026-05-11T01:44:56+00:00

I am getting a little confused and need some help please. Take these two

  • 0

I am getting a little confused and need some help please. Take these two classes

public class Comment {      public string Message {get; set;}      public DateTime Created {get; set;} }  public class Post {     public int PostId {get; set;}     public string Content {get; set;}     public IList<Comment> Comments {get; set;} } 

I want to write a linq query which returns a single Post but ordered by the comment created date.

So i started off constructing my linq query as follows:

var query = from p in _repository.GetPosts()                         where p.PostId == id                         orderby p.Comments.Select(x => x.Created)                         select p;              return query.Single(); 

But the orderby statement seem not to work! It just returns my list in the default sort order. Any suggestions on how i can make this work??? Thanks in advance!

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T01:44:56+00:00Added an answer on May 11, 2026 at 1:44 am

    Ordered by which comment date? the first? the last? You cuold try:

    orderby p.Comments.Max(x=>x.Created) 

    for example.

    Also – your Single suggests you expect exactly one row, in which case there isn’t much point sorting it. Do you mean First() ?


    Or do you mean that you want to sort the Comments? In which case, get the Post first;

    Post post = ... 

    Now… sorting the Comments is a little tricky because of your IList<T> – if you don’t mind it being a little inefficient, this is simple:

    post.Comments = post.Comments.OrderBy(x=>x.Created).ToList(); 

    Of course, if the Comments was List<T>, you could do:

    post.Comments.Sort((x, y) => (x.Created.CompareTo(y.Created))); 

    There are also tricks you can do to make an extension method of the form:

    post.Comments.Sort(x=>x.Created); 

    i.e.

    public static void Sort<TSource, TKey>(     this List<TSource> source,     Func<TSource, TKey> selector) {     var comparer = Comparer<TKey>.Default;     source.Sort((x, y) => comparer.Compare(selector(x), selector(y))); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a bit confused with this and need a little help please.
I'm getting a little confused how to set up a class that's two steps
I am getting a little tricked up right now so I need some help.
I need a little help getting this script doing what I want it to.
I'm getting a little confused about the Manager and Service class name suffix. As
I'm just getting started with Ember. I'm a little confused on some things, as
I'm getting a little confused with using Absolute and Relative positioning. Basically I have
@alex-r help me a lot with this but im getting a little issue. To
After spending roughly two days on this, I'm getting a little rattled. Although by
I'm getting into Google Web Toolkit, and am a little confused about the Entry

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.