我如何组合来自两个通用列表的数据并将该合并指定为 DataGridView 的数据源?
How can I combine data from two generic lists and assign that amalgamation as the datasource of a DataGridView?
我有两个 classes,AssignmentHistory 和 Students。我想像这样在 DataGridView 中显示 AssignmentHistory 的内容:
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
dataGridViewStudentHistory.DataSource = assignmentHistory;
但是AssignmentHistory class只有一个StudentID,没有名字;姓名在学生 class 中。我可以这样得到它:
Student student = StudentsList.FirstOrDefault(s => s.StudentID == studentId);
string fullName = $"{student.firstName} {student.lastName}";
(Student.StudentID对应AssignmentHistory.StudentID_FK)
如何将 ID 替换为全名,以便它显示在 DataGridView 中?
更新
好像不想"Include";我试过 "Union" 但也没有用:
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Union(AYttFMConstsAndUtils.StudentsList)
.OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
当我这样做时,OrderBy 中的 "s." 不允许 AssignmentHistList 或 StudentsList 的任何成员。
更新 2
我最终做的,至少是暂时的,是创建第三个 class,它结合了我在这种情况下从两个不同的通用列表中需要的数据。我循环遍历 assignmentHistory 的内容,并在必要时通过如下代码获取我需要的值:
Student student = GetStudentForStudentId(ah.studentId);
string fullName = student.FullName;
newClass.FullName = fullName;
...等然后我简单地使用这个新的、合并的通用列表,它由来自其他地方的点点滴滴组成,作为 DataGridView 的数据源。
如果学生可以访问 from/property of AssignmentHistList
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Include(x => x.Student)
.OrderBy(s => s.Student.StudentId).ThenBy(w => w.WeekOfAssignment).ToList();
这将包括来自 assignmenthistlist 和学生的所有字段
但如果不是
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Include(x => x.Student)
.OrderBy(s => s.Student.StudentId)
.ThenBy(w => w.WeekOfAssignment)
.Select(x =>
new { Field1 = x.field1,
Field2 = x.field2.....
StudentFirstName = StudentsList.Where(y => y.StudentId == x.AssignmentHistory.StudentID_FK).Select(c => c.FirstName).FirstOrDefault(),
StudentLastName = StudentsList.Where(y => y.StudentId == x.AssignmentHistory.StudentID_FK).Select(c => c.LastName).FirstOrDefault(),
StudentFullName = StudentsList.Where(y => y.StudentId == x.AssignmentHistory.StudentID_FK).Select(c => c.FirstName + " " + c.LastName).FirstOrDefault()}
.ToList();
我
我有两个 classes,AssignmentHistory 和 Students。我想像这样在 DataGridView 中显示 AssignmentHistory 的内容:
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
dataGridViewStudentHistory.DataSource = assignmentHistory;
但是AssignmentHistory class只有一个StudentID,没有名字;姓名在学生 class 中。我可以这样得到它:
Student student = StudentsList.FirstOrDefault(s => s.StudentID == studentId);
string fullName = $"{student.firstName} {student.lastName}";
(Student.StudentID对应AssignmentHistory.StudentID_FK)
如何将 ID 替换为全名,以便它显示在 DataGridView 中?
更新
好像不想"Include";我试过 "Union" 但也没有用:
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Union(AYttFMConstsAndUtils.StudentsList)
.OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
当我这样做时,OrderBy 中的 "s." 不允许 AssignmentHistList 或 StudentsList 的任何成员。
更新 2
我最终做的,至少是暂时的,是创建第三个 class,它结合了我在这种情况下从两个不同的通用列表中需要的数据。我循环遍历 assignmentHistory 的内容,并在必要时通过如下代码获取我需要的值:
Student student = GetStudentForStudentId(ah.studentId);
string fullName = student.FullName;
newClass.FullName = fullName;
...等然后我简单地使用这个新的、合并的通用列表,它由来自其他地方的点点滴滴组成,作为 DataGridView 的数据源。
如果学生可以访问 from/property of AssignmentHistList
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Include(x => x.Student)
.OrderBy(s => s.Student.StudentId).ThenBy(w => w.WeekOfAssignment).ToList();
这将包括来自 assignmenthistlist 和学生的所有字段
但如果不是
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Include(x => x.Student)
.OrderBy(s => s.Student.StudentId)
.ThenBy(w => w.WeekOfAssignment)
.Select(x =>
new { Field1 = x.field1,
Field2 = x.field2.....
StudentFirstName = StudentsList.Where(y => y.StudentId == x.AssignmentHistory.StudentID_FK).Select(c => c.FirstName).FirstOrDefault(),
StudentLastName = StudentsList.Where(y => y.StudentId == x.AssignmentHistory.StudentID_FK).Select(c => c.LastName).FirstOrDefault(),
StudentFullName = StudentsList.Where(y => y.StudentId == x.AssignmentHistory.StudentID_FK).Select(c => c.FirstName + " " + c.LastName).FirstOrDefault()}
.ToList();
我