我想使用谓词构建器加入两个表。 在伪代码中,我想返回所有StudentSchedule行,与StudentId上的Student一起加入,其中StudentLastName =" Smith"。
public class Student
{
int StudentId {get;set;}
string StudentFirstName {get;set;}
string StudentLastName {get;set;}
}
class StudentSchedule
{
int StudentScheduleId
int StudentId
string ClassName
}
我可以为一个实体做得很好 -
var studentBuilder = PredicateBuilder.True<Student>();
studentBuilder = studentBuilder.And(Student => Student.StudentId == studentId);
var students = context.Students.Where(studentBuilder).ToList();
答案 0 :(得分:1)
然后你应该把模型改成这样的东西:
trait
然后你的查询将是:
public class Student
{
public int StudentId {get;set;}
public string StudentFirstName {get;set;}
public string StudentLastName {get;set;}
public virtual ICollection<StudentSchedule> StudentSchedules {get;set;}
}
class StudentSchedule
{
public int StudentScheduleId {get;set;}
public int StudentId {get;set;}
public string ClassName {get;set;}
public virtual Student Student {get;set;}
}
没有谓词构建器:
var students = db.Students
.Include(s=>s.StudentSchedules)
.Where(studentBuilder)
.ToList();
我个人的偏好是不重复属性中的实体类型,除非它是外部属性,所以我的模型将是这样的:
var students = db.Students
.Include(s=>s.StudentSchedules)
.Where(s=>s.StudentLastName == "Smith")
.ToList();
当然,您可能需要一个更详细的模型,其中包含“主题”(名称,先决条件等),“课程”(主题,课堂,日程安排,员工),“学生”(FirstName,LastName,Classes) ,“Staff”(FirstName,LastName,JobTitle,Classes)和“Schedules”(星期几,开始时间,结束时间,类)。