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

  • Home
  • SEARCH
  • 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 981077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:29:04+00:00 2026-05-16T04:29:04+00:00

I am using GroupBy create a hierarchical set of groups to use in multiple

  • 0

I am using GroupBy create a hierarchical set of groups to use in multiple child grids.

Assume I have a query with with 6 columns, a, b, c, d, e, f.

Now, I need to group by a, then by b, then by c. and return the entire row in the group of c’s.

var q = rows.GroupBy(x => x.a)

Ok, that’s nice. That gives me my group of a’s. Next, we look to group them by a and b.

var q1 = q.Select(g =>new {
    Key = g.Key,
    SubGroup = g.GroupBy(x => x.b)
}

Ok, that also works nice. I get my group of a’s with subgroups of b’s.

Now I’m stumped at the third level. I’ve tried various syntaxes, but most won’t even compile. The ones that do do not give the correct results.

var q2 = q1.Select(g1 => new {
    Key = g1.Key,
    SubGroup = g1.GroupBy(x => x.c)
}

This doesn’t compile. Tells me that there is no GroupBy on g1.

var q2 = q.Select(g1 => new {
    Key = g1.Key,
    SubGroup = g1.GroupBy(x => x.c)
}

This doesn’t give me the b subgroup, only the a and c.

Any idea of what i’m doing wrong here?

EDIT:

The Following also does not work, saying there is no definition for the g1.Key

var q2 = q.Select(g => new {
    Key = g.Key,
    SubGroup = g.Select(g1 => new {
            Key = g1.Key
            SubGroup = g1.GroupBy(a => a.c)
        })

I have such a poor grasp on what this is doing internally.

  • 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-16T04:29:05+00:00Added an answer on May 16, 2026 at 4:29 am

    Now, I’m not saying this is actually a good approach; it’s probably going to be slow and the right way to do this, if performance matters, may be to sort the whole collection by these different criteria and then look at the different parts of the sorted collection.

    But if you want to use GroupBy and IGroupings to manage it, you’re working at it from the wrong end. You want to start at the deepest level first and work up.

    var groups = rows
        .GroupBy(x => new { x.A, x.B, x.C, x.D, x.E, x.F })
        .GroupBy(x => new { x.Key.A, x.Key.B, x.Key.C, x.Key.D, x.Key.E })
        .GroupBy(x => new { x.Key.A, x.Key.B, x.Key.C, x.Key.D, })
        .GroupBy(x => new { x.Key.A, x.Key.B, x.Key.C })
        .GroupBy(x => new { x.Key.A, x.Key.B })
        .GroupBy(x => x.Key.A);
    
    groups.First().Key;             // will be an A value
    groups.First().First().First(); // will be an A, B, C group
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to 'group by' a query using an alias, for example: select count(*), (select
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using JDeveloper , I started developing a set of web pages for a project
Now that I've read all the DQL docs I still have some doubts, I'm
I have this class public class Item { public Coordinate coordinate { get; set;
Can anyone help? I have a linq query which is embedded inside a extension
For some reason, when using two sums on a group by, i run into
Using ASP.NET MVC there are situations (such as form submission) that may require a

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.