日期:2014-05-20 浏览次数:20717 次
//中间表
public class Middle
{
[Key]
public int MiddleID { set; get; }
[ForeignKey("Student")]
public int StudentID { set; get; }
[ForeignKey("Subject")]
public int SubjectID { set; get; }
public virtual Student Student { set; get; }
public virtual Subject Subject { set; get; }
}
//学生表
public class Student
{
[Key]
public int StudentID { set; get; }
[Column("StudentName")]
public string StudentName { set; get; }
[Column("BeiZhu")]
public string BeiZhu { set; get; }
public virtual ICollection<Middle> Middle { set; get; }
}
//科目表
public class Subject
{
[Key]
public int SubjectID { set; get; }
[Column("SubjectName")]
public string SubjectName { set; get; }
public virtual ICollection<Middle> Middle { set; get; }
}
var query=(from m in db.Middle
group m by m.SubjectID into g
let cunt=g.Count(n=>n.StuentID)
orderby cunt descending
select new
{
SubjectName =db.Subject.SingleOrDefault(sj=>sj.SubjectID==g.Key).SubjectName,