Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用谓词生成器联接表_C#_Linq_Predicatebuilder_Predicates - Fatal编程技术网

C# 尝试使用谓词生成器联接表

C# 尝试使用谓词生成器联接表,c#,linq,predicatebuilder,predicates,C#,Linq,Predicatebuilder,Predicates,我想使用谓词生成器连接两个表。 在伪代码中,我希望返回所有StudentSchedule行,并与StudentId上的Student连接,其中StudentLastName=“Smith” 我可以为一个实体做,很好- var studentBuilder = PredicateBuilder.True<Student>(); studentBuilder = studentBuilder.And(Student => Student.StudentId == studentI

我想使用谓词生成器连接两个表。 在伪代码中,我希望返回所有StudentSchedule行,并与StudentId上的Student连接,其中StudentLastName=“Smith”

我可以为一个实体做,很好-

var studentBuilder = PredicateBuilder.True<Student>();

studentBuilder = studentBuilder.And(Student => Student.StudentId == studentId);

var students = context.Students.Where(studentBuilder).ToList();
var studentBuilder=predictebuilder.True();
studentBuilder=studentBuilder.And(Student=>Student.StudentId==StudentId);
var students=context.students.Where(studentBuilder.ToList();

然后您应该将您的模型更改为以下内容:

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;}
}
public class Student
{
    public int Id {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public virtual ICollection<StudentSchedule> Schedules {get;set;}
}
class StudentSchedule
{
    public int Id {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(s=>s.StudentLastName == "Smith")
  .ToList();
我个人的偏好是不在属性中重复实体类型,除非它是外部属性,因此我的模型如下:

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;}
}
public class Student
{
    public int Id {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public virtual ICollection<StudentSchedule> Schedules {get;set;}
}
class StudentSchedule
{
    public int Id {get;set;}
    public int StudentId {get;set;}
    public string ClassName {get;set;}
    public virtual Student Student {get;set;}
}
公共班级学生
{
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共虚拟ICollection调度{get;set;}
}
班级学生时间表
{
公共int Id{get;set;}
公共int StudentId{get;set;}
公共字符串类名{get;set;}
公共虚拟学生学生{get;set;}
}

当然,您可能需要一个更详细的模型,其中包含“科目”(姓名、先决条件等)、“班级”(科目、教室、时间表、员工)、“学生”(名字、姓氏、班级)、“员工”(名字、姓氏、职衔、班级)和“时间表”(星期几、开始时间、结束时间、班级).

为什么要使用谓词生成器进行此操作,当然可以使用标准查询进行此操作?学生和学生时间表之间的关系是什么?是一对一关系吗?唉,谓词生成器是我必须使用的。在这种情况下,一个学生可以有多个StudentSchedule。您应该为这些类添加导航属性。