public void EditStudent(FullInfoStudent student) { Users editUser = student; StudentGroups editSGUser = null; using (ElectronicalJournalContext db = new ElectronicalJournalContext()) { var studGroup = from sg in db.StudentGroups where sg.UserId == editUser.UserId select new StudentGroups { StudentGroupId = sg.StudentGroupId, UserId = sg.UserId, GroupId = sg.GroupId }; foreach (StudentGroups item in studGroup) { editSGUser = item; } if (editSGUser == null && student.GroupId != null) { StudentGroups sg = new StudentGroups(); sg.GroupId = student.GroupId; sg.UserId = student.UserId; db.StudentGroups.Add(sg); } else if (editSGUser == null) { } else if (editSGUser.GroupId != student.GroupId) { editSGUser.GroupId = student.GroupId; db.StudentGroups.Update(editSGUser); } db.Users.Update(editUser); db.SaveChanges(); } }
public FullInfoStudent FullInfoStudent(int id) { using (ElectronicalJournalContext db = new ElectronicalJournalContext()) { var student = from us in db.Users join al in db.AccessLevels on us.AccessLevelId equals al.AccessLevelId into alDatails from alDat in alDatails.DefaultIfEmpty() join stGr in db.StudentGroups on us.UserId equals stGr.UserId into stGrDatails from stGrDat in stGrDatails.DefaultIfEmpty() join gr in db.Groups on stGrDat.GroupId equals gr.GroupId into grDatails from grDat in grDatails.DefaultIfEmpty() where us.UserId == id select new FullInfoStudent { UserId = us.UserId, FirstName = us.FirstName, MiddleName = !string.IsNullOrEmpty(us.MiddleName) ? us.MiddleName : string.Empty, LastName = us.LastName, Login = us.Login, PasswordHash = us.PasswordHash, PasswordSalt = us.PasswordSalt, AccessLevelId = us.AccessLevelId, AcessLevelName = !string.IsNullOrEmpty(alDat.AccessLevelName) ? alDat.AccessLevelName : string.Empty, Email = us.Email, Phone = us.Phone, GroupId = stGrDat.GroupId, GroupName = !string.IsNullOrEmpty(grDat.GroupName) ? grDat.GroupName : string.Empty }; foreach (var item in student) { _fullInfoStudent = item; } } return(_fullInfoStudent); }