Hi,

I'm stuck trying to find a solution to this problem. Its a question taken from an exam for an interview.

I have this object model:

Department: DepartmentId, Name
TeacherTeacherId, FirstName, LastName, DateOfBirth, AnnualSalary, DepartmentId
CourseCourseId, Name, TeacherId, DepartmentId
Student: StudentId, FirstName, LastName, DateOfBirth, AverageScore, DepartmentId

Department has a 1 to many relationship with Teacher, Student and Course.

Teacher has a 1 to many relationship with Course

Student has a many to many relationship with Course

In a previous question I asked on here I asked how to create an interface between Student and Teacher called IPeople to highlight all commonality. I was pointed in the right direction and came up with

using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;

                namespace School.Code
                {
                    public interface IPeople
                    {
                        string FirstName
                        {
                            get;
                            set;
                        }
                        string LastName
                        {
                            get;
                            set;
                        }
                        Int64 DepartmentId
                        {
                            get;
                            set;
                        }
                        DateTime DateOfBirth
                        {
                            get;
                        }
            
                    }
                }

I then needed to create an IENumerable value of IPeople which I did using

public IEnumerable<IPeople> GetAllIPeople()
                {
                    foreach (IPeople person in GetAllTeachers())
                    {
                        yield return person;
                    }
                    foreach (IPeople person in GetAllStudents())
                    {
                        yield return person;
                    }
                }

How ever I am stuck with the next part of this.

The question is. Write method to get all IPeople for a Course - Write a method in the SchoolManager to get all IPeople for a particular Course. Your method should return an IENumerable of IPeople and make use of the existing GetAllIPeople method you've just written in the SchoolManager.

Now at first I thought this would be easy and should just use .where(c => c.CourseId == cId) where cId was passed into the method. How ever CourseId isnt in the interface as its not a common property between Student and Teacher. I do know because of the relationships between the objects that the Course, Student and Teacher will all share the same DepartmentID.

I am however completely lost as to how I produce an answer to this question. If anyone can point me in the right direction I would be grateful.

IEnumberable<IPeople> GetPeopleForCourse(int courseID) {
    foreach(IPeople person in GetAllPeople) {
        if (person is Student) {  // this might not work, but then again it might. Let me know as I'm not able to test here :)
            if (((Student)person).CourseID == courseID) {
                yield return person;
            }
        }
    }
}

Again, errors in names you'll have to fix :)

Hi momerath.

Once again thank you for the reply.

the line

if (((Student)person).CourseID == courseID)

doesnt work as there is not method for CourseId in Student. This is why I am getting confused as Teacher and Course don't have a CourseId. Course has a TeacherId and there is another table that doesn't display in the Object Model View called StudentCourses which is CourseId and StudentId. I presume this is the many to many link joining the two.

Never mind. I've edited the code you gave me and managed to get it working :)

public IEnumerable<IPeople> GetPeopleForCourse(int courseID)
               {
                   Course c = new Course();
                   foreach (IPeople person in GetAllIPeople())
                   {
                       if (person is Student)
                       {  // this might not work, but then again it might. Let me know as I'm not able to test here :)
                               if (((Student)person).Courses.Any(t => t.CourseId == courseID))
                               {
                                   yield return person;
                               }
                       
                       }
                       if (person is Teacher)
                       {
                           if (((Teacher)person).Courses.Any(t => t.CourseId == courseID))
                           {
                               yield return person;
                           }
                       }
                   }
                   
               }

Might not be elegant but it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.