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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:21:32+00:00 2026-06-13T23:21:32+00:00

I asked a question almost identical to this a few days ago via this

  • 0

I asked a question almost identical to this a few days ago via this thread and got awesome replies – The biggest lesson for me (aside from the answer itself) was to create a custom object for my holding data.
So, having now done that, can I ask you experts to please show me the most efficient Linq statement to satisfy my requirements?

My Scenario is as follows:

Suppose I have the following holding class:

Public Class Class_Info
    Public Property Teacher As String
    Public Property Name As String
    Public Property Sex As String
End Class

Then, suppose I have the following in another module:

Dim X as new list(of Class_Info)

With the following elements in the list:

Element.Teacher:     Element.Sex:        Element.Name:
Teacher 1            Male                Whatever Name 1
Teacher 2            Female              Whatever Name 2 
Teacher 1            Female              Whatever Name 3
Teacher 1            Female              Whatever Name 4
Teacher 2            Male                Whatever Name 5
Teacher 3            Male                Whatever Name 6
Teacher 3            Female              Whatever Name 7
Teacher 1            Male                Whatever Name 8
Teacher 1            Female              Whatever Name 9
Teacher 2            Male                Whatever Name 10

Now, suppose I want to create the following structure with the following values:

Dim dictTeacherSexName as New Dictionary(Of String, Dictionary(Of String, List(of String)))

Dict1_Key:        Dict1_Value / Dict2_Key:         Dict2_Value:
Teacher 1         Male                             Whatever Name 1
                                                   Whatever Name 8
                  Female                           Whatever Name 3
                                                   Whatever Name 4
                                                   Whatever Name 9
Teacher 2 ...

How could I create that via Linq in the most efficient way?

Thanks!!!

  • 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-13T23:21:32+00:00Added an answer on June 13, 2026 at 11:21 pm

    It is the same as last time, however instead of relying on indexes you can point at your object directly.

    var result = X.GroupBy(i => i.Teacher)
         .ToDictionary(g => g.Key, g => g
             .GroupBy(i => i.Sex)
             .ToDictionary(h => h.Key, h => h
                 .Select(i => i.Name)
                 .ToList()));
    

    You can also do this with a ILookup for cleaner use. You can use a custom class as your lookup object but it will need to implement IComparable.

    class Program
    {
        public static void Main()
        {
            var X = new List<Class_Info>();
    
            X.Add(new Class_Info { Teacher = "Teacher 1", Sex = "Male", Name = "Whatever Name 1" });
            X.Add(new Class_Info { Teacher = "Teacher 2", Sex = "Female", Name = "Whatever Name 2" });
            X.Add(new Class_Info { Teacher = "Teacher 1", Sex = "Female", Name = "Whatever Name 3" });
            X.Add(new Class_Info { Teacher = "Teacher 1", Sex = "Female", Name = "Whatever Name 4" });
            X.Add(new Class_Info { Teacher = "Teacher 2", Sex = "Male", Name = "Whatever Name 5" });
            X.Add(new Class_Info { Teacher = "Teacher 3", Sex = "Male", Name = "Whatever Name 6" });
            X.Add(new Class_Info { Teacher = "Teacher 3", Sex = "Female", Name = "Whatever Name 7" });
            X.Add(new Class_Info { Teacher = "Teacher 1", Sex = "Male", Name = "Whatever Name 8" });
            X.Add(new Class_Info { Teacher = "Teacher 1", Sex = "Female", Name = "Whatever Name 9" });
            X.Add(new Class_Info { Teacher = "Teacher 3", Sex = "Male", Name = "Whatever Name 10" });
    
            var result = X.ToLookup(key => new Tuple<string,string>(key.Teacher, key.Sex), value => value.Name);
    
            // Name is a IEnumerable<string> of all names who have "Teacher 1" and are Male.
            var name = result[new Tuple<string,string("Teacher 1","Male")];
        }
    
        public class Class_Info
        {
            public string Teacher;
            public string Name;
            public string Sex;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Few days ago I asked this question and got the answer: How to add
This is almost identical to this question asked by another user, and is the
It's almost a year ago that I asked this question on SO: Menu Accelerator
When I asked this question I got almost always a definite yes you should
This question was inspired by one I asked almost a year ago - any-orms-that-work-with-ms-access-for-prototyping
I looked through Stackoverflow and found almost identical question here . It was asked
I know this is a frequently asked question and I havent got a clear
I asked this question earlier and I got very quick and helpful responses: Linq
I asked this question: Is ASP.NET MVC destined to replace Webforms? and a few
I know that question with same title has been asked almost 6 month ago.

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.