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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:39:41+00:00 2026-05-22T17:39:41+00:00

I have a problem which I’ve simplified down and have been scratching my head

  • 0

I have a problem which I’ve simplified down and have been scratching my head at. If I have a list of students and I want to use LINQ to produce a group of students by Age, Grade, and LastName so I can traverse them with a nested foreach loop how would I go about this. Here is some code:

class Student
    {
        public int Grade;
        public int Age;
        public string LastName;
    }

    public void SomeMethod()
    {
        Student student1 = new Student() { Age = 10, Grade = 100, LastName = "Smith"};
        Student student2 = new Student() { Age = 10, Grade = 90, LastName = "Jones" };
        Student student3 = new Student() { Age = 10, Grade = 50, LastName = "Bob" };
        Student student4 = new Student() { Age = 10, Grade = 100, LastName = "Smith" };
        Student student5 = new Student() { Age = 11, Grade = 10, LastName = "Bob" };
        Student student6 = new Student() { Age = 11, Grade = 30, LastName = "Jones" };
        Student student7 = new Student() { Age = 13, Grade = 90, LastName = "Bob" };
        Student student8 = new Student() { Age = 13, Grade = 90, LastName = "Smithy" };
        Student student9 = new Student() { Age = 15, Grade = 100, LastName = "Smith" };
        Student student10 = new Student() { Age = 15, Grade = 0, LastName = "Smith" };

        List<Student> studentList = new List<Student>()
                                     {
                                         student1,student2,student3,student4,student5,student6,student7,student8,student9,student10
                                     };

        var studentGroups = from student in studentList
                            group student by student.Age into studentAgeGroup
                            from studentAgeGradeGroup in
                                              (
                                                  from student in studentAgeGroup
                                                  group student by student.Grade
                                              )
                            group studentAgeGradeGroup by studentAgeGroup.Key;

        foreach (var ageGroup in studentGroups)
        {                
            foreach (var gradeGroup in ageGroup)
            {
                foreach (var innerGroupElement in gradeGroup)
                {
                     // At this point they are grouped by age and grade but I would also like them to be grouped by LastName
                }
            }
        }
    }
  • 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-22T17:39:41+00:00Added an answer on May 22, 2026 at 5:39 pm

    How about?

    var studentGroups = from student in studentList
                group student by new {student.Age, student.Grade, student.LastName};
    

    and looping through each element in studentGroups, you can access Age by sg.Key.Age and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with listview which list item contain a checkbox. When i
I have a problem which requires me to parse several log files from a
I have a problem which I think is related to forward declarations, but perhaps
I have a very simple problem which requires a very quick and simple solution
I have a problem with a string in C++ which has several words in
I have a problem with an application which uses the same stored procedure over
I have a problem with a little .Net web application which uses the Amazon
I have a problem in my project with the .designer which as everyone know
I have a problem, which I do not seem to be able to solve...
The problem: I have a class which contains a template method execute which calls

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.