简体   繁体   English

如何组合来自两个通用列表的数据并将该合并分配为 DataGridView 的数据源?

[英]How can I combine data from two generic lists and assign that amalgamation as the datasource of a DataGridView?

I have two classes, AssignmentHistory and Students.我有两个班级,AssignmentHistory 和学生。 I want to show the contents of AssignmentHistory in a DataGridView like so:我想在 DataGridView 中显示 AssignmentHistory 的内容,如下所示:

List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
    .OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();
dataGridViewStudentHistory.DataSource = assignmentHistory;

But the AssignmentHistory class only has a StudentID, not a name;但是AssignmentHistory 类只有一个StudentID,没有名字; the name is in the Student class.该名称在 Student 类中。 I can get it like so:我可以这样理解:

Student student = StudentsList.FirstOrDefault(s => s.StudentID == studentId);
string fullName = $"{student.firstName} {student.lastName}";

(Student.StudentID corresponds to AssignmentHistory.StudentID_FK) (Student.StudentID 对应 AssignmentHistory.StudentID_FK)

How can replace the ID with the fullname so that it will display in the DataGridView?如何用全名替换 ID 以便它显示在 DataGridView 中?

UPDATE更新

It doesn't seem to want "Include";它似乎不想要“包含”; I tried "Union" but that didn't work, either:我试过“联盟”,但也没有用,要么:

List<AssignmentHistory> assignmentHistory = AYttFMConstsAndUtils.AssignmentHistList
                .Union(AYttFMConstsAndUtils.StudentsList)
                .OrderBy(s => s.StudentID_FK).ThenBy(w => w.WeekOfAssignment).ToList();

When I do that, the "s."当我这样做时,“s”。 in the OrderBy doesn't allow any member of either AssignmentHistList or StudentsList. OrderBy 中的任何成员都不允许 AssignmentHistList 或 StudentsList。

UPDATE 2更新 2

What I ended up doing, at least temporarily, is creating a third class which combines data I need for this situation from two different generic lists.我最终做的,至少是暂时的,是创建第三个类,它结合了我需要的来自两个不同通用列表的数据。 I loop through the contents of assignmentHistory and, where necessary, get the value I need through code like:我遍历 assignmentHistory 的内容,并在必要时通过如下代码获取我需要的值:

Student student = GetStudentForStudentId(ah.studentId);
string fullName = student.FullName;
newClass.FullName = fullName;

...etc. ...等等。 Then I simply use that new, amalgamated generic list, comprised of bits and pieces from elsewhere, as the datasource for the DataGridView.然后我简单地使用这个新的、合并的通用列表,由来自其他地方的点点滴滴组成,作为 DataGridView 的数据源。

If student is accessible from/property of AssignmentHistList如果学生可以从 AssignmentHistList 的/属性访问

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

this will include all the fields from assignmenthistlist and student这将包括 assignmenthistlist 和 student 中的所有字段

but if not但如果不是

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

I一世

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM