我一直很难找到一个以适当方式展示教室的考勤表。我的数据库中有一个出勤表,我可以很容易地使用来自一个教室的数据来显示它,但它的显示方式看起来就像您直接在数据库中浏览到出勤表时的样子。
通过展示我的工作可能更容易解释:
我有以下课程:
实际的课堂课程或课程:
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public int AttendanceDate { get; set;}
public virtual ICollection<Enrollment> Enrollments { get; set; } // allows Students to be enrolled in a Course
etc. . .
}
我的学生们:
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; } // allows Student to be enrolled in a Course
etc. . .
}
将学生与课程联系起来的实体:
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
出勤数据:
public class Attendance
{
public int AttendanceID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public int AttendanceDay { get; set; } // used to set how many days people are supposed to attend this Course (each course has a different length, some are 10 day courses, some are 3, etc.)
public bool Present { get; set; } // absent or present (set to absent by default)
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
我的项目流程让教师创建课程供学生注册。当学生注册课程时,所有必要的出勤数据都会以默认值(每天都缺席)输入到出勤数据库表中:
for (int i = 0; i < course.AttendingDays; i++)
{
Attendance newAttendance = new Attendance
{
CourseID = course.CourseID,
StudentID = thisStudent.StudentID,
AttendanceDay = i + 1,
Present = false
};
db.Attendance.Add(newAttendance);
db.Save();
}
所以我有一个包含一堆考勤数据的数据库表,我无法让它在屏幕上正确显示,排序类似于:
第一天出勤 | 2 | 3 | 4 | 5 |
学生 1 缺席 缺席 缺席 缺席 缺席
Student2 缺席 缺席 缺席 缺席
Student3 缺席 缺席 缺席 缺席
希望你能读到。. . 我认为这是一个非常标准的考勤表布局。
我尝试使用 Group By 对数据进行排序:
var model = from s in db.Attendance
where s.CourseID == 4
group s.AttendanceDay by s.StudentID into t
select new
{
StudentID = t.Key,
Days = t.OrderBy(x => x)
};
return View(model);
它返回一个匿名类型的 IEnumerable(如果我理解正确的话),但我似乎无法让这些数据做任何事情。如果我使用“更简单”的表生成器(并且不要尝试按 AttendanceDay 编号分组),我得到的出勤表数据就好了,但它的排序就像您查看出勤的实际数据库表时一样,不是很如果您希望教师阅读和编辑信息,这很有用。
我想我需要一个合适的 ViewModel 来调整匿名类型格式的 IEnumerable 中的传入考勤数据,然后是一个适当显示该 ViewModel 的视图。. . 但我不确定我将如何处理该过程。
任何帮助,将不胜感激。谢谢你。
更新:
我开始认为我需要利用“交叉表报告”/“数据透视”并使用如下内容:http ://linqlib.codeplex.com/wikipage?title=Pivot&referringTitle=Home
任何指针?
更新 2:
使用以下接受的答案几乎完全解决了,这是我当前的控制器:
// Generates list of Attendances specifically for current Course
var attendanceItems = db.Attendance.Where(s => s.CourseID == id);
List<Attendance> attendanceItemsList = attendanceItems.ToList();
// End of generating list of Attendances
// CURRENT PROBLEM AREA - INCOMPLETE
var student = attendanceItemsList.Select(a => a.Student).Distinct()/*.OrderBy(a => a)*/; // This works for adding one student, Cannot use OrderBy in its current state - how can I properly order by name? (right now it will order by id I believe)
List<Student> StudentList = student.ToList();;
//
// Generates list of AttendingDays specifically for current Course
Course course = db.Courses.FirstOrDefault(p => p.CourseID == id);
List<int> attDayList = new List<int>();
for (int i = 0; i < course.AttendingDays; i++)
{
attDayList.Add(i + 1);
};
// End of generating list of AttendingDays
AttendanceReportViewModel model = new AttendanceReportViewModel
{
AttendanceDays = attDayList,
Students = StudentList,
Attendances = attendanceItemsList,
};
return View(model);