日期:2014-05-17 浏览次数:20520 次
public class Teacher
{
public Guid ID { get; set; }
public string Name { get; set; }
[Display(Name = "班级ID")]
public Guid? GradeID { get; set; }
public virtual Grade Grades { get; set; }
}
public class Student
{
public Guid ID { get; set; }
public string Name { get; set; }
[Display(Name = "班级ID")]
public Guid GradeID { get; set; }
}
public class Grade
{
public Guid ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
var gen = db.Teachers.Include("Grades").ToList();
var gen = db.Grades.Include("Students").ToList();
select dbo.Student.Name from dbo.Teacher left join dbo.Grade on dbo.Grade.ID=dbo.Teacher.GradeID
left join dbo.Student on dbo.Student.GradeID=dbo.Grade.ID where dbo.Teacher.Name='老师1'
var query=from t in db.Teacher
where t.Name=="老师1"
join gd in db.Grade on gd.ID equals t.GradeID into leftGroup
from gd in leftGroup.DefalutIfEmpty()
join s in db.Student on s.GradeID equals gd.ID into lg2
from s in lg2.DefaultIfEmpty()
select new {Name=s==null?"":s.Name};