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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:11:42+00:00 2026-05-30T02:11:42+00:00

I have two Generic lists having objects of class : class Subject { public

  • 0

I have two Generic lists having objects of class :

class Subject
 {
   public string Code { get; set; }
   public string Name { get; set; }
 }

And another class as below :

class Student
{
    public string RollNo { get; set; }
    public string Name { get; set; 
}

And a List of type having Roll No and Subject codes as KeyValuePair for student as below :

List<KeyValuePair<string,string>>  RoleList=
           new  List<KeyValuePair<string,string>> ();

Student st1=new Student();          
Subject sb1=new Subject();
Subject sb2=new Subject();
Subject sb3=new Subject();

RoleList.Add(new KeyValuePair<string,string>(st1.RollNo,sb1.Code));
RoleList.Add(new KeyValuePair<string,string>(st1.RollNo,sb2.Code));
RoleList.Add(new KeyValuePair<string,string>(st1.RollNo,sb3.Code));

What I need is another Dictionary of type

Dictionary<string, List<Subject>> StSbList =
    new Dictionary<string, List<Subject>>();

where StSbList should have a list of subjects for given student using dictionary RoleList having all the subjects for a given Roll No. using LINQ in C# 4.0 , something like

 StSbList(st1.RollNo, {sb1,sb2,sb3});

I tried and was able merge to similar collections but struck with different type of collections. Any suggestions on any other better approach.

As rightly pointed by Douglas and Martin, both the solutions are working fine but on my test data ToLookup works faster so used the same.

Solution 1

Dictionary<string, List<Subject>> stSbList =
RoleList.GroupBy(kvp => kvp.Key)
        .ToDictionary(
            grouping => grouping.Key,
            grouping => grouping.Select(kvp => (from sub in listSub
                                                where sub.Code.Equals(kvp.Value)
                                                select sub).First()).ToList(),
            EqualityComparer<string>.Default);

Solution 2

var tSbList = RoleList.ToLookup(kvp => kvp.Key, kvp => 
             (from sub in listSub
              where sub.Code.Equals(kvp.Value)
              select sub).First());
  • 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-30T02:11:44+00:00Added an answer on May 30, 2026 at 2:11 am

    Your question isn’t really clear. If you’re asking for an example of how to populate your dictionary, here’s one:

    Dictionary<string, List<Subject>> stSbList = new Dictionary<string, List<Subject>>();
    stSbList.Add(st1.RollNo, new List<Subject> { sb1, sb2, sb3 });
    stSbList.Add(st2.RollNo, new List<Subject> { sb1, sb3 });
    stSbList.Add(st3.RollNo, new List<Subject> { sb1, sb2 });
    

    More succinctly using nested collection initializers:

    Dictionary<string, List<Subject>> stSbList = new Dictionary<string, List<Subject>>
    {
        { st1.RollNo, new List<Subject> { sb1, sb2, sb3 } },
        { st2.RollNo, new List<Subject> { sb1, sb3 } },
        { st3.RollNo, new List<Subject> { sb1, sb2 } },
    };
    

    Update: As others have pointed out, your definition of RoleList as a dictionary is probably erroneous, since it will not allow you to define multiple subjects for the same student. What you need is a data structure that supports the representation of a many-to-many relation; for example, a List<T> of Tuple<string,string>, where Item1 of each tuple contains the Student.RollNo, whilst Item2 contains the Subject.Code:

    var roleList = new List<Tuple<string, string>>();
    roleList.Add(Tuple.Create(st1.RollNo, sb1.Code));
    roleList.Add(Tuple.Create(st1.RollNo, sb2.Code));
    roleList.Add(Tuple.Create(st1.RollNo, sb3.Code));
    roleList.Add(Tuple.Create(st2.RollNo, sb1.Code));
    roleList.Add(Tuple.Create(st2.RollNo, sb3.Code));
    roleList.Add(Tuple.Create(st3.RollNo, sb1.Code));
    roleList.Add(Tuple.Create(st3.RollNo, sb2.Code));
    

    To convert this into a dictionary, you could use first use the GroupBy operator, followed by ToDictionary:

    var subjects = new[] { sb1, sb2, sb3 }.ToDictionary(sb => sb.Code);
    
    Dictionary<string, List<Subject>> stSbList =
        roleList.GroupBy(tuple => tuple.Item1)
                .ToDictionary(
                    grouping => grouping.Key,
                    grouping => grouping.Select(tuple => subjects[tuple.Item2]).ToList(),
                    EqualityComparer<string>.Default);
    

    Update2: For completeness, this is how to construct the dictionary from your definition of RoleList (although this probably isn’t what you want):

    var roleList = new Dictionary<string, string>();
    roleList.Add(st1.RollNo, sb1.Code);
    roleList.Add(st2.RollNo, sb2.Code);
    roleList.Add(st3.RollNo, sb2.Code);
    
    var subjects = new[] { sb1, sb2, sb3 }.ToDictionary(sb => sb.Code);
    
    Dictionary<string, List<Subject>> stSbList = roleList.ToDictionary(
        kvp => kvp.Key,
        kvp => new List<Subject> { subjects[kvp.Value] });
    

    Update3: Adapted to work with the latest version of your code:

    var subjects = new[] { sb1, sb2, sb3 }.ToDictionary(sb => sb.Code);
    
    Dictionary<string, List<Subject>> stSbList =
        RoleList.GroupBy(kvp => kvp.Key)
                .ToDictionary(
                    grouping => grouping.Key,
                    grouping => grouping.Select(kvp => subjects[kvp.Value]).ToList());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Generic Lists with the same objects Type T within them. For
I have two classes public class JobDataProvider { public List<Job> Get(int id){ List<Job> jobs
I have two threads, a producer thread that places objects into a generic List
I'm having trouble grasping generic methods. I have two classes that are generated (they
I have two generic list objects, in which one contains ids and ordering, and
I have two EmailAddress generic lists, I wanted a simple way of just getting
Let's say I have two generic lists of the same type. How do I
I have two generic lists of type T. Both lists contain same type, and
I have two generic lists. Let's say they are List< A > and List<
I have two lists of custom objects and want to update a field for

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.