I have been having too much trouble designing a query to retrieve questions that a candidate have already attempted and another query for the questions that a candidate has not attempted. This is for a examination/test/form/survey type application.
The scenario is a candidate(OAS_UserDetail) is associated with many groups(OAS_Group). One group can have many tests. A test can have many questions. A question can have many options.
When a candidate attempts a question it is stored in table TestResponse having reference to the session, (the answerSelected in table TestResponse is actually the QuestionOption.Id). I believe TestSession can act as a bridge for TestResponse to get the details of user, test and group.
This seems to me a good but somewhat complex design for me as far as querying is concerned through Linq. Below is what I tried to do and got stuck and end up writing Linq in method syntax instead of query syntax.
OAS.DataModels.OAS_Question questionsAttempted =
from q in db.OAS_Questions
where q.OAS_Test.OAS_Group.Candidates.Contains(
db.OAS_UserDetails.Single(u => u.UserName == HttpContext.User.Identity.Name)
)
select q;
Linq query syntax is very similar to SQL. You should be able to do what you need using "join" operators.
Here's a good overview:
So you are looking for questions of a candidate of which the options either are or aren't in the candidate's TestResponse
records. I think this could work:
(from u in OAS_UserDetail
from g in u.OAS_Group
from t in g.OAS_Test
from q in t.OAS_Question
from o in q.OAS_QuestionOption
where u.Id == userId // a variable
where u.OAS_TestSession
.SelectMany(s => s.OAS_TestResponse)
.Select(r => r.AnswerSelectedId).Contains(o.Id)
select q).Distinct()
and for the questions that the candidate did not attempt: where !u.OAS_TestSession...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.