I store the results of the survey in a table, and I need to find out the latest answers for all questions for a specific quiz for the user (each line has id, quizid, questionid, answerid, username and datecompleted).
I earned, but itβs so ugly, I thought that I would ask for help to optimize it. I am starting to resolve my years ago, writing the best quality code! :) So, if someone wants to tell me how to optimize it, that would be very useful!
public List<QuestionResult> GetLatestResult(Guid QuizID, string UserName)
{
List<QuestionResult> quizResult = new List<QuestionResult>();
var questionIDs = (from q in db.QuizResults
where (q.QuizId == QuizID && q.UserName == UserName)
select q.QuestionId).Distinct();
var results = from r in questionIDs
select (from q in db.QuizResults
where q.QuizId == QuizID
&& q.UserName == UserName
&& q.QuestionId == r
orderby q.DateCompleted descending
select q).Take(1);
foreach (var item in results)
{
foreach (var qr in item)
{
QuestionResult result = new QuestionResult();
result.QuestionId = qr.QuestionId;
result.AnswerId = qr.AnswerId;
quizResult.Add(result);
}
}
return quizResult;
}
This is C #, linq to sql, let me know if you need more details.
Thank,
Anneli
source
share