public int Delete(int ID)
        {
            tblAssignmentRoutine tb = _db.tblAssignmentRoutines.Where(a => a.Routine_Id == ID).FirstOrDefault();

            _db.tblAssignmentRoutines.Remove(tb);
            return(_db.SaveChanges());
        }
        public int Add(AssignmentRoutineViewModel arvm)
        {
            tblAssignmentRoutine tb = new tblAssignmentRoutine();

            tb.Assignment_Name         = arvm.Assignment_Name;
            tb.Teacher_Id              = arvm.Teacher_Id;
            tb.Assignment_Release_Date = arvm.Assignment_Release_Date;
            tb.Deadline    = arvm.Deadline;
            tb.Section_Id  = arvm.Section_Id;
            tb.Faculty_Id  = arvm.Faculty_Id;
            tb.Semester_Id = arvm.Semester_Id;
            tb.YearBatchId = arvm.Year_Batch_Id;


            _db.tblAssignmentRoutines.Add(tb);
            _db.SaveChanges();


            tblStudentRoutineRelation tsr = new tblStudentRoutineRelation();
            var studnet = _db.tblStudents.Where(s => s.Faculty_Id == tb.Faculty_Id && s.Section_Id == tb.Section_Id && s.Semester_Id == tb.Semester_Id && s.YearBatchId == tb.YearBatchId).ToList();

            foreach (var item in studnet)
            {
                tsr.RoutineId = tb.Routine_Id;
                tsr.StudentId = item.Student_Id;
                _db.tblStudentRoutineRelations.Add(tsr);
                _db.SaveChanges();
            }
            return(0);
        }
        public bool StudentAssignmentMatch(int?StudentId, int?RoutineId)
        {
            tblStudent           ts = _db.tblStudents.Where(s => s.Student_Id == StudentId).FirstOrDefault();
            tblAssignmentRoutine tr = _db.tblAssignmentRoutines.Where(r => r.Routine_Id == RoutineId).FirstOrDefault();

            if (ts.Faculty_Id == tr.Faculty_Id && ts.YearBatchId == tr.YearBatchId /*&& ts.Section_Id == tr.Section_Id && ts.Semester_Id == tr.Semester_Id*/)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public int Update(AssignmentRoutineViewModel arvm)
        {
            bool fieldChange        = false;
            tblAssignmentRoutine tb = _db.tblAssignmentRoutines.Where(a => a.Routine_Id == arvm.Id).FirstOrDefault();

            if (tb.Section_Id == arvm.Section_Id && tb.Faculty_Id == arvm.Faculty_Id && tb.Semester_Id == arvm.Semester_Id && tb.YearBatchId == arvm.Year_Batch_Id)
            {
                fieldChange = false;
            }
            else
            {
                fieldChange = true;
            }
            tb.Assignment_Name         = arvm.Assignment_Name;
            tb.Teacher_Id              = arvm.Teacher_Id;
            tb.Assignment_Release_Date = arvm.Assignment_Release_Date;
            tb.Deadline    = arvm.Deadline;
            tb.Section_Id  = arvm.Section_Id;
            tb.Faculty_Id  = arvm.Faculty_Id;
            tb.Semester_Id = arvm.Semester_Id;
            tb.YearBatchId = arvm.Year_Batch_Id;
            _db.SaveChanges();
            //tblStudentRoutineRelation tsr = new tblStudentRoutineRelation();
            //var studnet = _db.tblStudents.Where(s => s.Faculty_Id == tb.Faculty_Id && s.Section_Id == tb.Section_Id && s.Semester_Id == tb.Semester_Id && s.YearBatchId == tb.YearBatchId).ToList();
            if (fieldChange)
            {
                var studentlist = _db.tblStudentRoutineRelations.Where(s => s.RoutineId == tb.Routine_Id).ToList();
                foreach (var item in studentlist)
                {
                    _db.tblStudentRoutineRelations.Remove(item);
                }
                tblStudentRoutineRelation tsr = new tblStudentRoutineRelation();
                var studnet = _db.tblStudents.Where(s => s.Faculty_Id == tb.Faculty_Id && s.Section_Id == tb.Section_Id && s.Semester_Id == tb.Semester_Id && s.YearBatchId == tb.YearBatchId).ToList();
                foreach (var item in studnet)
                {
                    tsr.RoutineId = tb.Routine_Id;
                    tsr.StudentId = item.Student_Id;
                    _db.tblStudentRoutineRelations.Add(tsr);
                    _db.SaveChanges();
                }
            }

            return(0);
        }