public List<Tra_TraineeChangeClassEntity> ChangeClass(List<Tra_TraineeChangeClassEntity> lstTraineeChangeClass) { var lstTraineeData = new List<Tra_TraineeChangeClassEntity>(); using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var lstClassOldCode = lstTraineeChangeClass.Select(m => m.ClassOldCode).Distinct().ToList(); var lstClassNewCode = lstTraineeChangeClass.Select(m => m.ClassNewCode).Distinct().ToList(); var lstCodeEmp = lstTraineeChangeClass.Select(m => m.CodeEmp).Distinct().ToList(); var lstClassOld = unitOfWork.CreateQueryable<Tra_Class>(m => lstClassOldCode.Contains(m.Code)).Select(m => new { m.ID, m.CourseID, m.Code }).ToList(); var lstClassNew = unitOfWork.CreateQueryable<Tra_Class>(m => lstClassNewCode.Contains(m.Code)).Select(m => new { m.ID, m.CourseID, m.Code }).ToList(); var lstTraineeInDb = unitOfWork.CreateQueryable<Tra_Trainee>(m => lstClassOldCode.Contains(m.Tra_Class.Code) && lstCodeEmp.Contains(m.Hre_Profile.CodeEmp)).ToList(); foreach (var item in lstTraineeChangeClass) { string ClassOldID = item.ClassOldCode; string ClassNewID = item.ClassNewCode; Guid CourseOldID = lstClassOld.Where(m => m.Code == ClassOldID).Select(m => m.CourseID).FirstOrDefault(); Guid CourseNewID = lstClassNew.Where(m => m.Code == ClassNewID).Select(m => m.CourseID).FirstOrDefault(); if (CourseOldID != CourseNewID) continue; //var traineeInDB = lstTraineeInDb.Where(m => m.Hre_Profile.CodeEmp == item.CodeEmp && m.Tra_Class.Code == ClassOldID).FirstOrDefault(); //if(traineeInDB!= null) //{ // traineeInDB.Tra_Class1.Code = ClassNewID; //} Tra_TraineeChangeClassEntity trainee = new Tra_TraineeChangeClassEntity(); trainee.CodeEmp = item.CodeEmp; trainee.ClassOldCode = item.ClassOldCode; trainee.ClassNewCode = item.ClassNewCode; lstTraineeData.Add(trainee); } } return lstTraineeData; }
public JsonResult SaveChangeClass([DataSourceRequest] DataSourceRequest request, CatImportModel model) { var services = new Tra_TraineeServices(); List<ImportTraineeEntity> lstTrainee = new List<ImportTraineeEntity>(); List<Tra_TraineeChangeClassEntity> lstTraineeTranslate = new List<Tra_TraineeChangeClassEntity>(); List<Tra_TraineeScoreImport> DataError = new List<Tra_TraineeScoreImport>(); var _fileName = Common.GetPath(Common.TemplateURL) + model.TemplateFile; _fileName = _fileName.Replace("/", "\\"); ImportService = new ImportService { FileName = _fileName, DateTimeFormat = model.FormatDate, ImportTemplateID = model.ID, }; try { var table = ImportService.ImportNew(_fileName, model.TemplateFile, model.ID); lstTrainee = table.Translate<ImportTraineeEntity>(); foreach (var item in lstTrainee) { var entity = new Tra_TraineeChangeClassEntity { CodeEmp = item.Column2, ClassOldCode = item.Column3, ClassNewCode = item.Column4 }; lstTraineeTranslate.Add(entity); } var message = services.ImportChangeClass(lstTraineeTranslate,UserLogin); if (message == NotificationType.Success.ToString()) { return Json(message, JsonRequestBehavior.AllowGet); } } catch (Exception ex) { model.Description = ex.Message; } return Json(DataError.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); }