使用C#进行课程安排

时间:2015-10-29 09:50:07

标签: c# asp.net-mvc entity-framework linq-to-entities

我正在为一所学校安排课程,为可用的课堂分配课程。我有这样的树表(简化):

TCourse:CourseID,CourseCode,Hours,Capacity,ScheduleID ......

TClassroom:ClassroomID,ClassroomName,Quota ...

TCourseClassroom:CourseClassroomID,CourseID,ClassroomID,StartHour,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次。我无法想象出这个目的的最佳方式。我想更新教室使用数小时的数据库或者我应该只用代码吗?任何想法都会有所帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

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

  1. 删除.FirstOrDefault();句子,因此在var classRoom中你有一个Enumerable,其中包含课程所需的所有课程。
  2. Foreach教室,在包含此教室的TCourseClassroom所有CourseClassroom中找到并使用EndHour进行订购。
  3. 检查您是否课程时数加上CourseClassroom的EndHour小于20,如果是,您可以将课程列入本课程。
  4. 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");
    }
    

    }