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
  • 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. 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

My little brother is just getting into programming, and for his Science Fair project,
So I'm getting really confused on how to do this whole thing and I
I am really getting confused on how pointers work. I am trying to write
I am a little confused about the session management in PHP. A lot of
I'm a little confused with an issue that I keep running into. I'm trying
Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
Getting started with jquery and having trouble getting hello world type example going for
Without getting a degree in information retrieval, I'd like to know if there exists
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
I getting the following error when I try to connect to my server app

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.