2

In my asp.net app I have a class that contains InstructorID and InstructorName.
In the old code, I have a System.Collections.Hashtable that contains InstructorName (value) and StudentID (key).

What I would like to do is add the studentID to my class object (where InstructorName = InstructorName), and given that one instructor can have many students associated, however, one student can only have one instructor..

My class objects is:

public class ClassInstructor
{
    public int InstructorID { get; set; }
    public string InstructorName { get; set; }
    public List<string> studentID { get; set; }
}

How would I accomplish that?
Or is there a better way of doing this?

MVC Newbie
  • 23
  • 5

1 Answers1

1

If your hash table is a Dictionary where the key is the StudentID and the value is the InstructorName, you could do the following:

Dictionary<string, string> studentTeachers = new Dictionary<string, string>();
studentTeachers.Add("Student #1", "Instructor #1");
studentTeachers.Add("Student #2", "Instructor #1");
studentTeachers.Add("Student #3", "Instructor #1");
studentTeachers.Add("Student #4", "Instructor #1");
studentTeachers.Add("Student #5", "Instructor #2");
studentTeachers.Add("Student #6", "Instructor #2");
studentTeachers.Add("Student #7", "Instructor #2");
studentTeachers.Add("Student #8", "Instructor #2");

var instructors = new List<ClassInstructor>();

instructors.Add(new ClassInstructor() { InstructorID = 1, InstructorName = "Instructor #1" });
instructors.Add(new ClassInstructor() { InstructorID = 2, InstructorName = "Instructor #2" });

foreach (var instructor in instructors)
    instructor.Students = studentTeachers.Where(x => x.Value == instructor.InstructorName).Select(x => x.Key).ToList();

Since you didn't post a lot of code you may need to adapt this to work with your specific objects. This is also assuming that the collection of instructors has already been populated and that you are replacing any references to students that may already exist. It's also assuming that your instructor name strings will match exactly, if not you may need to perform a more lenient string comparison.

Basically in this code you're using LINQ to populate each Students collection with a list of strings that you projected from the hash table based on matching keys.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • Your assumptions above are correct.Thanks for your help. I will try this out and let you know. – MVC Newbie Oct 01 '13 at 16:36
  • If I am following your code, you are converting 'students' from Hashtable into a dictionary, is that correct? – MVC Newbie Oct 01 '13 at 16:50
  • No... I'm converting a Dictionary to a list of strings that can be added to each `ClassInstructor`, assuming that your StudentID -> InstructorName collection is a Dictionary. You didn't specify which class your hash table is represented by. – Trevor Elliott Oct 01 '13 at 17:08
  • The hashtable is a 'System.Collections.Hashtable'. When I run your code, I get an error on Where .... System.Collections.Hashtable' does not contain a definition for 'Where' – MVC Newbie Oct 01 '13 at 19:41
  • Right, Hashtable is not an IEnumerable collection so it doesn't support LINQ. You can copy your Hashtable values to a dictionary like the one above by using a statement like `foreach (var key in hashTable.Keys) studentTeachers.Add(key, hashTable[key]);` Hashtable is like a very old version of a Dictionary before .NET 2.0 so you probably don't want to use it anyway. – Trevor Elliott Oct 01 '13 at 19:48
  • Thanks @TrevorElliott. That works. I do have one more quick question. Based on your code above, if any of the records do NOT have a studentID associated with a Instructor, (meaning instructor.Students... count = 0...) how would I go about deleting that collection (by that I mean the InstructorName, InstructorID, ListStudentID) ? Thanks again. – MVC Newbie Oct 01 '13 at 21:20
  • One way is after adding the students to the instructors list you could write `instructors = instructors.Where(x => x.Students.Any()).ToList()`, which is a way of saying *"Pluck from the instructors collection only the instructors which have **any** students and replace the instructors list with a new list containing those plucked instructors."* – Trevor Elliott Oct 02 '13 at 04:12
  • 1
    You could also do `foreach (var instructor in instructors.ToArray()) { if (instructor.Students.Count == 0) instructors.Remove(instructor); }` In that case you have to convert the instructors to an array to enumerate through them because you can't modify a collection while you are enumerating through it in a foreach. However, I prefer the above method using LINQ as it's more succinct. – Trevor Elliott Oct 02 '13 at 04:15