English 中文(简体)
筛选接口外具有类属性的接口的IENumerable
原标题:Filter IENumerable of Interface with property of class outside of interface

我一直在想办法解决这个问题。这是一道面试考试题。我会把这个标记为家庭作业,因为我的最后一个问题是这样标记的。

我有这个对象模型:

部门:部门Id,名称

教师:教师ID,名字,姓氏,出生日期,年薪,部门ID

课程:课程ID,名称,教师ID,部门ID

学生:StudentId,FirstName,LastName,DateOfBirth,AverageScore,DepartmentId

教师学生课程有一对多关系。

Teacher has a 1 to many relationship with Course *Student* has a many to many relationship with Course

在我之前在这里问的一个问题中,我问了如何在学生和老师之间创建一个名为IPeople的界面,以突出所有的共性。我被指向了正确的方向,想出了

            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;
                    }

                }
            }

然后我需要创建一个IENumerable值的IPeople,我使用

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

我怎么会被这件事的下一部分卡住。

问题是。编写方法以获取某门课程的所有I人员-在SchoolManager中编写方法以获得特定课程的所有IP人员。您的方法应该返回一个IPeople的IENumerable,并使用您刚刚在SchoolManager中编写的现有GetAllIPeople方法。

一开始我觉得这很容易,应该使用where(c=>;c.CourseId==cId),其中cId被传递到方法中。However CourseId不在界面中,因为它不是学生和教师之间的共同属性。我知道,由于对象之间的关系,课程、学生和教师都将共享同一个部门ID。

然而,我完全不知道该如何回答这个问题。如果有人能为我指明正确的方向,我将不胜感激。

将Microsoft Visual Studio 2010与MS Entity Framework 4.0一起使用

最佳回答

如果你把它看作是一个拥有人们的课程,而不是试图从人们那里发现课程,你就不会面临这个问题。因此,在元代码中:

Course course=....
foreach(IPeople person in course.Students) //or whatever
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签