如何使用LINQ从3个相关实体获取数据?

时间:2017-07-02 21:20:40

标签: c# sql entity-framework linq

我有一个学生班,课程班和会话班。我想根据 rollno 会话名称 programName 密码来选择学生。
课程和会话课与学生班有一对多的关系。

这是会话类。

public class Session
    {
        [Key]
        public int SessionID { get; set; }
        [Required]
        [MaxLength(30)]
        public string SessionName { get; set; }

        //Relationship ------- Navigational Properties -------------------------------
        public virtual List<Student> Students { get; set; }
        public virtual List<Teacher> Teachers { get; set; }
        public virtual List<Subject> Subjects { get; set; }
        public virtual List<Program> Programs { get; set; }
    }

这是程序类。

public class Program
    {
        [Key]
        public int ProgramID { get; set; }
        [Required]
        [MaxLength(30)]
        public string ProgramName { get; set; }

        //Relationship ------- Navigational Properties -------------------------------
        public virtual List<Student> Students { get; set; }
        public virtual List<Teacher> Teachers { get; set; }
        public virtual List<Subject> Subjects { get; set; }

    }

这是学生班。

public class Student
    {
        [Key]
        [Column(Order = 0)]
        public int StudentID { get; set; }
        [Required]
        [MaxLength(30)]
        public string FirstName { get; set; }
        [Required]
        [MaxLength(30)]
        public string LastName { get; set; }
        [Required]
        [MaxLength(30)]
        public string UserName { get; set; }
        [Required]
        [MaxLength(35)]
        public string Email { get; set; }
        [Required]
        [MaxLength(30)]
        public string Password { get; set; }
        [Required]
        [MaxLength(30)]
        public string FatherName { get; set; }
        [Required]
        public DateTime DOB { get; set; }
        [Required]
        [MaxLength(15)]
        public string CNIC { get; set; }
        [Required]
        public int RollNo { get; set; }

        public bool Active { get; set; }
        public bool Graduated { get; set; }
        public bool Expelled { get; set; }

        //Relationship ------- Navigational Properties -------------------------------
        public virtual List<Teacher> Teachers { get; set; }
        public virtual List<Subject> Subjects { get; set; }
        public virtual List<StudentMessage> Messages { get; set; }

        public Session Session { get; set; }
        public Program Program { get; set; }
        public Grade Grade { get; set; }
        public Attendance Attendance { get; set; }
        public StudentContact StudentContact { get; set; }
        public StudentMessage StudentMessage { get; set; }

    }

现在如何选择使用LINQ查询语法并使用LINQ方法语法提供特定rollno,sessionName,programName,密码的学生用户名?

在数据库中,学生表包含SessionID和ProgramID。

我知道&#34;加入&#34;用于从多个表中提取数据,但我不知道如何在LINQ语法中使用它。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

var list = dbContext.Students
    .Where(e=>e.RollNo == 10)
    .Where(e=>e.Password == "password")
    .Where(e=>e.Session.SessionName == "SessionName")
    .Where(e=>e.Session.ProgramName == "ProgramName")
    .Select(e=> e.UserName)
    .ToList();