Philip Smith
Philip Smith

Reputation: 25

Filter IENumerable of Interface with property of class outside of interface

I'm stuck trying to find a solution to this problem. Its a question taken from an exam for an interview. I will mark this as homework as my last question was tagged for me that way.

I have this object model:

Department: DepartmentId, Name

Teacher: TeacherId, FirstName, LastName, DateOfBirth, AnnualSalary, DepartmentId

Course: CourseId, 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.

Using Microsoft Visual Studio 2010 with MS Entity Framework 4.0

Upvotes: 0

Views: 376

Answers (1)

spender
spender

Reputation: 120480

If you think of it in terms of a Course owning people rather than trying to discover courses from people, you won't face this problem. So in meta-code:

Course course=....
foreach(IPeople person in course.Students) //or whatever

Upvotes: 1

Related Questions