【发布时间】:2016-03-10 06:04:20
【问题描述】:
我有两个课程,AssignmentHistory 和 Student。我想在 DataGridView 中显示 AssignmentHistory 的内容,如下所示:
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
dataGridViewStudentHistory.DataSource = assignmentHistory;
但是AssignmentHistory 类只有StudentID,没有名字;该名称在 Student 类中。我可以这样得到它:
Student student = StudentsList.FirstOrDefault(s => s.StudentID == studentId);
string fullName = $"{student.firstName} {student.lastName}";
(Student.StudentID对应AssignmentHistory.StudentID_FK)
如何将 ID 替换为全名,以便在 DataGridView 中显示?
更新
它似乎不需要“包含”;我尝试了“Union”,但也没有用:
List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
.Union(AYttFMConstsAndUtils.StudentsList)
.OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
当我这样做时,“s”。在 OrderBy 中不允许 AssignmentHistList 或 StudentsList 的任何成员。
更新 2
我最终做的,至少是暂时的,是创建第三个类,它结合了我在这种情况下需要的来自两个不同通用列表的数据。我遍历 assignmentHistory 的内容,并在必要时通过以下代码获取我需要的值:
Student student = GetStudentForStudentId(ah.studentId);
string fullName = student.FullName;
newClass.FullName = fullName;
...等等。然后,我只需使用由其他地方的零碎组成的新的合并通用列表作为 DataGridView 的数据源。
【问题讨论】:
-
您使用的是 WPF 还是 WinForms?
-
WinForms,可以肯定。
标签: c# linq datagridview lambda generic-list