private async Task <List <Document> > getDocumentsForStudent(Exam exam, Student student, string ip) { List <Document> documents; if (!await db.Assignments.AnyAsync(a => a.StudentId == student.Id && db.Documents.Any(d => d.Id == a.DocumentId && db.Questions.Any(q => q.Id == d.QuestionId && q.ExamId == exam.Id)))) { //we need to do this in one student at a time, so if multiple students hit the server //at the same time, we'll still process them sequentially await slim.WaitAsync(); try { //now that we know we haven't already given you documents //figure out which documents to hand over documents = await AssignmentHandler.GetStudentDocuments(db, student, exam, ip, random); await saveDocuments(student, ip, documents); } finally { slim.Release(); } } else { //send them the same files again documents = await db.Documents.Where(d => db.Assignments.Any(a => a.DocumentId == d.Id && a.StudentId == student.Id && db.Questions.Any(q => d.QuestionId == q.Id && db.Exams.Any(e => e.Id == q.ExamId && e.Id == exam.Id)))).ToListAsync(); await saveDocuments(student, ip, documents); } return(documents); }