【问题标题】:How can I combine data from two generic lists and assign that amalgamation as the datasource of a DataGridView?如何组合来自两个通用列表的数据并将该合并指定为 DataGridView 的数据源?
【发布时间】: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


【解决方案1】:

如果学生可以从 AssignmentHistList/属性访问

List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
                                           .Include(x => x.Student)
                                           .OrderBy(s => s.Student.StudentId).ThenBy(w => w.WeekOfAssignment).ToList();

这将包括 assignmenthistlist 和 student 中的所有字段

如果不是

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();

【讨论】:

  • 查看我的更新;也许我做错了,但我无法让它工作(似乎没有“包含”可用;我也用“联合”尝试过
  • @B.ClayShannon 如果我再次编辑答案可以吗?
  • 杰维斯!端口号?
猜你喜欢
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2022-11-23
  • 2019-07-20
相关资源
最近更新 更多