This is a teacher objects
List<Teacher> teachername = new List<Teacher>();
Teacher teach = new Teacher();
teachername.Add(new Teacher("malathy", 2341, 987452165,0));
teachername.Add(new Teacher("shobhana", 3432, 4512789,1));
teachername.Add(new Teacher("indira", 6432, 243254564,2));
This is students object
teach.studentlist.Add(new Student("manu", 1, "babu",2341));
teach.studentlist.Add(new Student("anu", 1, "sabu", 2341));
teach.studentlist.Add(new Student("hiba", 2, "vasu", 3432));
teach.studentlist.Add(new Student("shuba", 2, "gopi", 3432));
teach.studentlist.Add(new Student("indu", 3, "soman",6432));
teach.studentlist.Add(new Student("indira", 3, "Raman", 6432));
I want to separate and control the students by its numbers,ie,the 1 numbers students are under teacher malathy.2 number students are under shobhana.
CodePudding user response:
Since we have no information about name's of objects properties, I'll just give them my own.
Option 1: foreach
Update: As @DavideVitali mentioned in the comment section, it's and option that there is no implementation of the IEnumerbale
, so I'll go with the foreach
.
foreach(var item in teach)
{
if(item.TeacherID == 1)
{
List<Student> MalathyStudents = new List<Student>();
MalathyStudents.Add(item);
}
if(item.TeacherID == 2)
{
List<Student> ShobhanaStudents = new List<Student>();
ShobhanaStudents.Add(item);
}
if(item.TeacherID == 3)
{
List<Student> IndiraStudents = new List<Student>();
IndiraStudents.Add(item);
}
}
Option 2: Linq.
Note: You will have to import using System.Linq;
I've changed the values of the last element in your objects, removed the 0 -
teachername.Add(new Teacher("malathy", 2341, 987452165, 1));
teachername.Add(new Teacher("shobhana", 3432, 4512789, 2));
teachername.Add(new Teacher("indira", 6432, 243254564, 3));
var MalathyStudents = teach.Where(s => s.TeacherID == 1).ToList();
var shobhanaStudents = teach.Where(s => s.TeacherID == 2).ToList();
var indiraStudents = teach.Where(s => s.TeacherID == 3).ToList();
CodePudding user response:
var MalathysStudents = studentlist.Where(s=>s.TeacherId==1).ToList();