public ActionResult StartAllotClass(Models.ClassAllotResult.StartAllotClass vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var del = (from p in db.Table <Basis.Entity.tbClassAllotResult>()
                           where p.tbClassAllotClass.tbYear.Id == vm.YearId
                           select p).ToList();
                foreach (var a in del)
                {
                    a.IsDeleted = true;
                }

                var classAllotStudentList = (from p in db.Table <Basis.Entity.tbClassAllotStudent>()
                                             orderby p.Score descending
                                             select new
                {
                    p.tbStudent,
                    ClassTypeId = p.tbClassType.Id,
                    GradeId = p.tbGrade.Id,
                    SexId = p.tbStudent.tbSysUser.tbSex != null ? p.tbStudent.tbSysUser.tbSex.Id : 0,
                    Score = p.Score
                }).ToList();
                if (vm.IsScore)
                {
                    classAllotStudentList = (from p in db.Table <Basis.Entity.tbClassAllotStudent>()
                                             orderby p.tbStudent.EntranceScore descending
                                             select new
                    {
                        p.tbStudent,
                        ClassTypeId = p.tbClassType.Id,
                        GradeId = p.tbGrade.Id,
                        SexId = p.tbStudent.tbSysUser.tbSex != null ? p.tbStudent.tbSysUser.tbSex.Id : 0,
                        Score = p.tbStudent.EntranceScore
                    }).ToList();
                }

                var classAllotClassList = (from p in db.Table <Basis.Entity.tbClassAllotClass>()
                                           where p.tbYear.Id == vm.YearId
                                           select new
                {
                    p,
                    GradeId = p.tbGrade.Id,
                    ClassTypeId = p.tbClassType.Id
                }).ToList();

                var classTypeList = (from p in db.Table <Basis.Entity.tbClassType>()
                                     orderby p.No
                                     select p.Id).ToList();
                var sexList = (from p in db.Table <Dict.Entity.tbDictSex>()
                               orderby p.No
                               select p.Id).ToList();
                sexList.Add(0);
                var list = new List <Basis.Entity.tbClassAllotResult>();

                foreach (var grade in classAllotClassList.Select(d => d.GradeId).Distinct())
                {
                    foreach (var classType in classTypeList)
                    {
                        foreach (var sex in sexList)
                        {
                            var studentListTemp = classAllotStudentList.Where(d => d.GradeId == grade && d.ClassTypeId == classType && d.SexId == sex).ToList();
                            var classListTemp   = classAllotClassList.Where(d => d.GradeId == grade && d.ClassTypeId == classType).ToList();

                            if (vm.IsOrder)
                            {
                                for (int i = 0; i < studentListTemp.Count; i++)
                                {
                                    var classAllotResult = new Basis.Entity.tbClassAllotResult()
                                    {
                                        tbStudent         = studentListTemp[i].tbStudent,
                                        tbClassAllotClass = classListTemp[i % classListTemp.Count].p,
                                        Score             = studentListTemp[i].Score,
                                    };
                                    list.Add(classAllotResult);
                                }
                            }
                            else
                            {
                                bool b = false;
                                for (int i = 0; i < studentListTemp.Count; i++)
                                {
                                    if (i % classListTemp.Count == 0)
                                    {
                                        b = !b;
                                    }

                                    var classAllotResult = new Basis.Entity.tbClassAllotResult()
                                    {
                                        tbStudent = studentListTemp[i].tbStudent,
                                        Score     = studentListTemp[i].Score
                                    };

                                    if (b)
                                    {
                                        classAllotResult.tbClassAllotClass = classListTemp[i % classListTemp.Count].p;
                                    }
                                    else
                                    {
                                        var num = (classListTemp.Count - 1) - i % classListTemp.Count;
                                        classAllotResult.tbClassAllotClass = classListTemp[num].p;
                                    }

                                    list.Add(classAllotResult);
                                }
                            }
                        }
                    }
                }

                db.Set <Basis.Entity.tbClassAllotResult>().AddRange(list);

                if (db.SaveChanges() > 0)
                {
                    XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("分班");
                }

                return(Code.MvcHelper.Post());
            }
        }
        public ActionResult StartAllotClass()
        {
            var vm = new Models.ClassAllotResult.StartAllotClass();

            return(View(vm));
        }