Optimize linq to sql query with subquery to get latest result

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>();

        // first get all question ids for that quiz
        var questionIDs = (from q in db.QuizResults
                          where (q.QuizId == QuizID && q.UserName == UserName)
                          select q.QuestionId).Distinct();

        // then get the most recent answer id for those questions
        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

+3
source share
2 answers

var questionIDsassignment can be removed - you already are filtering QuizID, and UserNamein the second.

, LINQ:

public List<QuestionResult> GetLatestResult(Guid QuizID, string UserName) {
    return (
           from q in db.QuizResults
           where q.QuizId == QuizID && q.UserName == UserName
           group q by q.QuestionId into grouped
           select new QuestionResult {
               QuestionId = grouped.Key,
               AnswerId = grouped.OrderByDescending(q => q.CompletionDate).First().AnswerId
           };
       ).ToList();
}

Edit

, q - , group q by q.QuestionId first q , .

+2

. , . , IEnumerable. , , .

public List<QuestionResult> GetLatestResult(Guid QuizID, string UserName)     
{         
    // get the latest date that this user took this quiz
    var latestQuizDate = (from q in db.QuizResults 
                          where q.QuizId == QuizID
                              && q.UserName == UserName
                          select q).Max(q => q.CompletionDate);

    // get the quiz results for this user/quiz/date 
    var results = from q in db.QuizResults
                  where q.CompletionDate = latestQuizDate
                      && q.QuizId == QuizID
                      && q.UserName == UserName
                  select new QuestionResult {q.QuestionId, q.AnswerId};

    return results.ToList();
} 
0

Source: https://habr.com/ru/post/1782610/


All Articles