Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C代码中使用LINQ读取icollection数据_C#_Linq_Entity Framework - Fatal编程技术网

C# 在C代码中使用LINQ读取icollection数据

C# 在C代码中使用LINQ读取icollection数据,c#,linq,entity-framework,C#,Linq,Entity Framework,我需要使用Linq查询为每个记录实例获取Icollection数据和其他数据,我做得很好 课程模式 方法读取记录 类模型 问题的第三部分 我现在得到的结果是正确的;我可以使用以下语句查看学生和他们注册的课程ID列表 using (var db2 = new MyDbContext()) { List<Student> _studentRead = new List<Student>(); _studen

我需要使用Linq查询为每个记录实例获取Icollection数据和其他数据,我做得很好

课程模式 方法读取记录 类模型 问题的第三部分 我现在得到的结果是正确的;我可以使用以下语句查看学生和他们注册的课程ID列表

  using (var db2 = new MyDbContext())
        {
            List<Student> _studentRead = new List<Student>();

            _studentRead = (from _student in db2.Students.Include(r=>r.StudentCourses)        
                            select _student).ToList();
        }
现在,如果我想在下面的图表中显示我可以阅读CourseID,那么我如何将课程标题包括在我的这个linq语句中


如果在加载student对象时尝试加载StudentCourse集合,则可以使用Include扩展方法

public void LoadStudents()
{
  using (var db2 = new MyDbContext()
  {
    //Lambda Linq
    var studentList = new List<Student>();
    studentList = db2.Students
                     .Include(s => s.StudentCourses)
                     .OrderBy (s => s.StudentId)
                     .ToList();

    //Comprehension Linq 
    var compList = new List<Student>();
    compList = (from s in db2.Students.Include(r => r.StudentCourses)
               OrderBy s.StudentId
               Select s).ToList();
  }
}

这是使用linq。然而,它使用的是lambda-linq语法。我已经更新了我的答案,包括一个使用理解的解决方案,我发现以下错误;无法将lambda表达式转换为类型“string”,因为它不是委托类型。请确保引用了实体框架并添加了using语句。否则,该函数将需要包含的属性的字符串名称。非常感谢,我已成功运行。但是,如果我想从不同的表中选择课程标题,然后选择不同的模型,我如何才能包含该名称。。。我已经更新了问题第三部分下的问题……您是否可以添加您的学生课程模型和您试图包含的实体模型,以增加问题的清晰度。谢谢,问题是什么?你说你需要什么,而不是你被困在哪里。
public partial class StudentCourse
{
    public int StudentCourseID { get; set; }
    public int StudentID { get; set; }
    public int CourseID { get; set; }

    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}
   public void readStudents()
    {
        using (var db2 = new MyDbContext())
        {
            List<Student> _studentRead = new List<Student>();

            _studentRead = (from b in db2.Students
                            orderby b.StudentID
                            select b).ToList() ;

        }
    }
 public partial class Student
{
    public Student()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int StudentID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}
  using (var db2 = new MyDbContext())
        {
            List<Student> _studentRead = new List<Student>();

            _studentRead = (from _student in db2.Students.Include(r=>r.StudentCourses)        
                            select _student).ToList();
        }
public void LoadStudents()
{
  using (var db2 = new MyDbContext()
  {
    //Lambda Linq
    var studentList = new List<Student>();
    studentList = db2.Students
                     .Include(s => s.StudentCourses)
                     .OrderBy (s => s.StudentId)
                     .ToList();

    //Comprehension Linq 
    var compList = new List<Student>();
    compList = (from s in db2.Students.Include(r => r.StudentCourses)
               OrderBy s.StudentId
               Select s).ToList();
  }
}
public void LoadStudents()
{
  //Lambda Linq
  var lambdaList = db2.Students
                      .Include(s => s.StudentCourses.Select(sc => sc.Course))
                      .OrderBy(s => s.StudentId)
                      .ToList();

 //Comprehension Linq
 var compList = (from student in db2.Students.Include(s => s.StudentCourse.Select(sc => sc.Course)
                 OrderBy student.StudentId
                 Select student).ToList(); 
}