Asp.net mvc 多对多关系中针对EF的LINQ到实体

Asp.net mvc 多对多关系中针对EF的LINQ到实体,asp.net-mvc,linq,linq-to-entities,entity-framework-5,Asp.net Mvc,Linq,Linq To Entities,Entity Framework 5,我正在使用ASP.NET MVC4 EF CodeFirst 需要帮助在索引操作中编写LINQ(到实体)代码,以获取所选学生参加的课程集合。该关系是多对多与连接表与有效负载之间的关系 我得到的错误是: The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Sel

我正在使用ASP.NET MVC4 EF CodeFirst

需要帮助在索引操作中编写LINQ(到实体)代码,以获取所选学生参加的课程集合。该关系是多对多与连接表与有效负载之间的关系

我得到的错误是:

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
我有一些模型(第三个是用于带负载的联接表):

//模型类
//-------------
公立班学生
{
公共int StudentId{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection学生课程{get;set;}
}
公共课
{
public int CourseId{get;set;}
公共字符串标题{get;set;}
公共虚拟ICollection学生课程{get;set;}
}
公办班学生上课
{
public int StudentToCourseId{get;set;}
public int FkStudentId{get;set;}
public int FkCourseId{get;set;}
公共字符串{get;set;}
公共虚拟学生对象{get;set;}
公共虚拟课程对象{get;set;}
}
然后,这里是我需要传递给视图的modelview

//VIEWMODEL CLASS
//---------------

public class StudentIndexViewModel
{
    public IEnumerable<Student> Students { get; set; }
    public IEnumerable<Course> Courses { get; set; }
    public IEnumerable<StudentToCourse> StudentsToCourses { get; set; }
}
//VIEWMODEL类
//---------------
公共班级学生索引十六模型
{
公共IEnumerable学生{get;set;}
公共IEnumerable课程{get;set;}
公共IEnumerable StudentsToCourses{get;set;}
}

EF不支持条件包含。您需要包含全部内容或不包含任何内容(即在
包含
中没有
Where

如果只需要获取某些关系的数据,可以将其选择为匿名类型,例如(显然未测试)


显然,这将需要一些代码更改,因为它不再是StudentsToCourses集合的直接课程。

谢谢!我正在考虑使用ViewBag将中间变量传递给view。也许“AsNumerable()”应该替换为“ToList()”?你怎么看?@Branislav
aseneumerable()
可以被
ToList()
替换,是的。唯一的缺点是,如果继续对结果进行筛选,则创建
List()
可能是不必要的开销,应该在应用所有筛选之后进行。
//MODEL CLASSES
//-------------

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<StudentToCourse> StudentsToCourses { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }

    public virtual ICollection<StudentToCourse> StudentsToCourses { get; set; }
}

public class StudentToCourse
{
    public int StudentToCourseId { get; set; }
    public int FkStudentId { get; set; }
    public int FkCourseId { get; set; }
    public string Classroom { get; set; }

    public virtual Student ObjStudent { get; set; }
    public virtual Course ObjCourse { get; set; }
}
//VIEWMODEL CLASS
//---------------

public class StudentIndexViewModel
{
    public IEnumerable<Student> Students { get; set; }
    public IEnumerable<Course> Courses { get; set; }
    public IEnumerable<StudentToCourse> StudentsToCourses { get; set; }
}
var intermediary = (from course in db.Courses
                    from stc in course.StudentsToCourses
                    where stc.ObjStudent.FkStudentId == id.Value
                    select new {item, stc}).AsEnumerable();