void StudentGroupCombo() { SqlConnection conn = new SqlConnection(myconnstring); //Connect to DB string sql = "SELECT DISTINCT GroupID from StudentGroup"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader StudentRead; try { conn.Open(); StudentRead = cmd.ExecuteReader(); while (StudentRead.Read()) { string GroupID = StudentRead["GroupID"].ToString(); cmdGroupMS.Items.Add(GroupID); } } catch (Exception ex) { } finally { conn.Close(); } }
/* * Instantiate a StudentRead Object and then finds the currentuser in that Instances, we * then assign that User to the Object and then perform a LINQ command to find the Users who * have read that page, once done return that list to get Distinct.Count to find the total of * people who have viewed that page and display who haven't read it. */ private IEnumerable <string> HowMany(Anouncement Anouncement) { StudentRead Read1 = new StudentRead(); string CurrentUser = User.Identity.GetUserId(); ApplicationUser Users = db.Users.FirstOrDefault(x => x.Id == CurrentUser); var UsersInRole = db.Roles.SingleOrDefault(r => r.Name == "Student").Users; var LectId = db.Roles.SingleOrDefault(l => l.Name == "Lecturer"); var Students = UsersInRole.Count; if (User.IsInRole("Student")) { Read1.AnnounceId = Anouncement; Read1.UserId = Users; db.StudentRead.Add(Read1); db.SaveChanges(); } var Count = (from db in db.StudentRead where db.AnnounceId.Id == Anouncement.Id select db.UserId.Id).AsEnumerable(); var Seen = (from db in db.StudentRead where db.AnnounceId.Id == Anouncement.Id select db.UserId).ToList(); var AllUsers = db.Users.ToList(); var Lecturer = db.Users.Where(Lec => Lec.Roles.Select(Rol => Rol.RoleId).Contains(LectId.Id)); var RemovedNots = AllUsers.Except(Seen); ViewBag.NotRead = RemovedNots.Except(Lecturer); ViewBag.Seen = Math.Round(100f * ((float)Seen.Distinct().Count() / (float)Students)); return(Count); }