如何使用 LINQ 使用两个数据表填充嵌套(父/子)列表

我是 Linq 和 C# 的新手。我正在使用两个数据表填充两个嵌套列表。有一个 StudentList 是父列表。每个学生的每门课程都有一个 GradeList。我可以从数据库中获取两个数据集作为数据表,一个是学生,另一个是年级。在 Grade 类中有一个可以与 Student 链接的 studentID。如何填充 StudentList?


    public class Student

{

    public string ID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Email { get; set; }

    public List<Grade> GradeList { get; set; }


}

  public class Grade

{

    public string CourseID { get; set; }

    public string CourseTitle { get; set; }

    public decimal CourseGrade { get; set; }

    public string studentID { get; set; }

}

//下面是代码


DataTable dt = ds.Tables[0];  //students

        DataTable dt1 = ds.Tables[1]; //Grades


        studList = (from DataRow dr in dt.Rows

                        select new Student()

                        {

                            ID = dr["ID"].ToString(),

                            FirstName = dr["FIRSTNAME"].ToString(),

                            LastName = dr["LASTNAME"].ToString(),

                            GradeList = (from DataRow gdr in dt1.Rows as IEnumerable

                                       //  where gdr["studentID"] == ID

                                         select new Grade() 

                                         {


                                             CourseID = gdr["COURSEID"].ToString(),

                                             CourseTitle = gdr["COURSETITLE"].ToString(),

                                             CourseGrade = Convert.ToDecimal(gdr["COURSEGRADE"]),

                                             studentID = gdr["STUDENTID"].ToString()

                                         }.ToList()


                                         )



                        }).ToList();

我期待填写 studList。非常感谢


鸿蒙传说
浏览 64回答 1
1回答

aluckdog

尝试这个:studList =&nbsp;&nbsp; &nbsp; (from DataRow dr in dt.Rows&nbsp; &nbsp; &nbsp;select new Student()&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ID = dr["ID"].ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FirstName = dr["FIRSTNAME"].ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LastName = dr["LASTNAME"].ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Email = dr["EMAIL"].ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GradeList =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(from DataRow gdr in dt1.Rows as IEnumerable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where gdr["studentID"] == dr["ID"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new Grade()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CourseID = gdr["COURSEID"].ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CourseTitle = gdr["COURSETITLE"].ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CourseGrade = Convert.ToDecimal(gdr["COURSEGRADE"]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; studentID = gdr["STUDENTID"].ToString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList()&nbsp; &nbsp; &nbsp; }).ToList();第一个ToList()需要在右括号之后,where条件 forGradeList需要引用对象初始值设定项之外的变量,因为Student对象尚未完全构造。
打开App,查看更多内容
随时随地看视频慕课网APP