答案 0 :(得分:0)
一种方法是使用实体框架的Include()函数。在您的考勤实体中,您需要与学生建立联系,例如在Attendance.cs:
public int StudentId { get; set; }
在学生实体中,您有一组参加者:
Student.cs
ICollection<Attendance> Attendances { get; set; }
在课堂上与学生联系:
public int StudentId { get; set; }
public Student Student { get; set; }
现在您必须将这些实体的集合添加到数据库上下文中:
ApplicationDbContext.cs
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext() : base("context")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Attendance> Attendances { get; set; }
public DbSet<Class> Classes { get; set; }
}
您现在可以使用以下连接实体加载您的实体:
using (ApplicationDbContext db = new ApplicationDbContext())
{
var result = db.Classes.Where( (clas) => clas.Value == SomeValue)
.Include( (clas) => clas.Student
.Include( (student) => student.Attendances
.Where( (attendance) => attendance.Month == "November") ));
}