使用C#进行课程安排

本文关键字:课程安排 使用 | 更新日期: 2023-09-27 18:19:53

我正在为一所学校做一个时间表项目,为可用的教室分配课程。我有这样的树表(简化):

T课程:CourseID,CourseCode,学时,容量,日程ID。。。

TClassroom:ClassroomID,ClassroomName,Quota。。。

T课程教室:CourseClassroomID,CourseID,ClassroomIDStartHour,FinishHour,ScheduleID

08:00-20:00之间的时间

下面的代码逐个获取课程,并为其分配容量最接近的教室,然后更新教室以表明教室已使用。(代码翻译成英文供您阅读和理解)

    public ActionResult Schedule()
    {
        int[] hours = { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        using (ApsContext db = new ApsContext())
        {
            List<TCourse> courses = new List<TCourse>();
            List<TClasssroom> clasRooms = new List<TClasssroom>();
            int lastScheduleId = 0;
            courses = db.TCourse.OrderBy(x => x.Quota).ToList();
            foreach (var itemCourse in courses)
            {
                var clasRoom = db.Classroom.Where(x => x.PlanID == lastScheduleId && x.Capacity >= itemCourse.Quota).OrderBy(x => x.Capacity).FirstOrDefault();
                if (classRoom != null)
                {
                    var courseId = itemCourse.CourseID;
                    var classRoomId = clasRoom.ClassroomID;
                    var scheduleId = itemCourse.ScheduleID + 1;
                    db.Entry(classRoom).Entity.ScheduleID = scheduleId;
                    db.SaveChanges();
                    CourseClassroom newCourseClassroom = new CourseClassroom();
                    newCourseClassroom.CourseID = courseId;
                    newCourseClassroom.ClasssroomID = classRoomId;
                    newCourseClassroom.ScheduleID = scheduleId;
                    db.TCourseClassroom.Add(newCourseClassroom);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Schedule");
        }
    }

这段代码只使用一节课一次,但在一天中,这节课有12个小时。想想所有的课程都是2小时。所以我一天要用6次那间教室。我想不出达到这个目的的最佳方式。我想用小时来更新教室使用的数据库,还是应该用代码来更新?任何想法都会有帮助。提前谢谢。

使用C#进行课程安排

我有一个解决方案,但可能不是最快的。

  1. 删除.FirstOrDefault();句子,这样在var classRoom中就有了一个Enumerable,其中包含了该课程所需的所有大类
  2. 对于每个教室,在TCourseClassroom中找到包含该教室的所有CourseClassroom,并用EndHour对其进行排序
  3. 检查你的课程时间加上课程教室的结束时间是否小于20,如果是,你可以把你的课程放在这门课上

public ActionResult Schedule(){

int[] hours = { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
using (ApsContext db = new ApsContext())
{
    List<TCourse> courses = new List<TCourse>();
    List<TClasssroom> clasRooms = new List<TClasssroom>();
    int lastScheduleId = 0;
    courses = db.TCourse.OrderBy(x => x.Quota).ToList();
    foreach (var itemCourse in courses)
    {
        var classRoomList = db.Classroom.Where(x => x.PlanID == lastScheduleId && x.Capacity >= itemCourse.Quota).OrderBy(x <= x.Capacity);
        TClasssroom availableClassRoom;
        foreach(TClasssroom classRoom in classRoomList)
        {
            TCourseClassroom courseClassroomList = db.TCourseClassroom.FindAll(x => x.ClassroomID == clasRoom.ClassroomID).OrderBy(x > x.EndHour).First();

            if(courseClassroomList == null)
            {
                availableClassRoom = classRoom;
            }
            else
            {
                if(courseClassroomList.EndHour + itemCourse.Hour <= 20)
                {
                    availableClassRoom = classRoom;
                }
            }   
        }
        if (availableClassRoom != null)
        {
            var courseId = itemCourse.CourseID;
            var classRoomId = clasRoom.ClassroomID;
            var scheduleId = itemCourse.ScheduleID + 1;
            db.Entry(classRoom).Entity.ScheduleID = scheduleId;
            db.SaveChanges();
            CourseClassroom newCourseClassroom = new CourseClassroom();
            newCourseClassroom.CourseID = courseId;
            newCourseClassroom.ClasssroomID = classRoomId;
            newCourseClassroom.ScheduleID = scheduleId;
            db.TCourseClassroom.Add(newCourseClassroom);
            db.SaveChanges();
        }
    }
    return RedirectToAction("Schedule");
}

}