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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:01:33+00:00 2026-06-06T09:01:33+00:00

How do I build an expression that will preserve the ThenBy order? The Expression

  • 0

How do I build an expression that will preserve the ThenBy order?
The Expression below produces the Groups ordered by Bowler.Number but the Matches of each group are not sorted.

public class Match
{
   public int Id { get; set; }
   public virtual Bowler Bowler { get; set; }
   public byte GameNumber { get; set; }
   public int Score { get; set; }
   ...
}

public class Bowler
{
   public int Id { get; set; }
   public int Number { get; set;}
   ...
}

var GroupedMatches = db.Matches.OrderBy(m=>m.Bowler.Number).ThenBy(m=>m.GameNumber).GroupBy(m => m.Bowler.Number)

Here is the output I want:

1
  game1 295
  game2 199
  game3 202
  game4 178
2
  game1 177
  ...

Current I use two foreach like

foreach (var item in TheGroups){
    ... do some stuff with the Group
    foreach (var x in item.OrderBy(a =>a.Number)) { //<-- can I get rid of this OrderBy?
    ... do some stuff with the Matches in this group
   }
}

No big deal, I just thought LINQ would be able to help me sort the matches in the group when the groups where built, not later when I process them.

  • 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-06-06T09:01:35+00:00Added an answer on June 6, 2026 at 9:01 am

    I’m not sure what you want the output to be, since it’s kind of non-sensical to order a list of groups keyed on bowler number by game number.

    Assuming you want an ordered list of bowler numbers, each containing an ordered list of games, something like this might work

    var GroupedMatches = db.Matches
        .GroupBy(m => m.Bowler.Number)
        .OrderBy(g => g.Key)
        .Select (g => new 
                { 
                    BowlerNumber = g.Key, 
                    Matches = g.OrderBy(m => m.GameNumber).ToList() 
                });
    

    Here’s specifically how you get the output you’re looking for

    foreach(var group in GroupedMatches) {
        Console.WriteLine(group.BowlerNumber);
        foreach(var match in group.Matches) {
            Console.WriteLine("game{0} {1}", match.GameNumber, match.Score);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a lambda expression that will be combined with others
I'm trying to build a regular expression that will catch one of three patterns
I'm looking to build a regular expression that will allow me to replace the
I am trying to build up an expression that will be applied to an
I'm attempting to build a regular expression that will match against the contents of
I understand that CDT 7 will have a regular expression error parser included, but
I'm trying to build a regular expression that will detect any character that Windows
I am trying to build an extension method that will Paginated a query. But
I'm trying to build an expression tree ( still ) but getting further! I
I am trying to build a function that will properly quote/escape an attribute in

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.