Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:22:20+00:00 2026-05-26T17:22:20+00:00

I have a design where we store answers to scripts’s questions by given users.

  • 0

I have a design where we store answers to scripts’s questions by given users.
One script can have many questions and each question can be answered multiple times by a given user.

The MS SQL tables look (removing extra details) more or less like:

-Scripts
ScriptId int (PK, identity)

-ScriptQuestions
ScriptId int    (PK)
QuestionId int  (PK)

-Questions
QuestionId int (PK, identity)
QuestionText varchar

-Answers
AnswerId int (PK, identity)
QuestionId int
UserId  int
AnswerText varchar

I would like to query this database for a given script and a given user and obtain all questions and the last answer provided for each question (if any).
In T-SQL I would do something like this:

SELECT 
    sq.QuestionId,
    q.QuestionText,
    la.AnswerText   
FROM    
    ScriptQuestions sq
        ON s.ScriptId = sq.ScriptId
    INNER JOIN Questions q
        ON sq.QuestionId = q.QuestionId
    LEFT OUTER JOIN (
            SELECT
                QuestionId,
                AnswerText
            FROM Answers
            WHERE AnswerId IN (
                        SELECT 
                            MAX(AnswerId) LastAnswerId
                        FROM Answers
                        WHERE UserId = @userId
                        GROUP BY QuestionId
                    )
            ) la
        ON q.QuestionId = la.QuestionId
WHERE
    sq.ScriptId = @scriptId

(untested, but I think it’s close to what I would do)

I am using LinqToEF on an MVC 3 application, and to get those results I used:

        var questions = from sq in script.ScriptQuestions
                        select new QuestionsAnswers
                                   {
                                       QuestionId = sq.QuestionId,
                                       QuestionText = sq.Question.QuestionText,
                                       LastAnswer = sq.Question.Answers
                                           .Where(a => a.UserId == userId)
                                           .OrderByDescending(a => a.AnswerId)
                                           .Select(a => a.AnswerText)
                                           .FirstOrDefault()
                                   };

And I do get the same results BUT when I run the Intellitrace profiler from VS 2010 I can see that linq translates this into sending a SELECT statement for EVERY QUESTION on the script and then ANOTHER SELECT statement for every answer. So if the scripts has 20 questions, it would be querying the database at least 40 times instead of sending just one SQL statement as above.

I tried to change the way I create my LinqToEF statement but I was not able to overcome the n SELECT statements issue.

This cannot be the right way, or is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:22:21+00:00Added an answer on May 26, 2026 at 5:22 pm

    My guess is that your query uses LINQ to Objects in memory in combination with lazy loading of the navigation properties because your query starts with script.ScriptQuestions which is apparently not an IQueryable. So the collection is iterated in memory and for every entry you access the sq.Question and sq.Question.Answers navigation properties. If lazy loading is enabled each time you access these properties a new query is issued to the DB to populate the properties. Your filter on the sq.Question.Answers collection is performed in memory on the full collection.

    You can try to change it the following way:

        var questions = from sq in context.ScriptQuestions
                        where sq.ScriptId == script.ScriptId
                        select new QuestionsAnswers
                                   {
                                       QuestionId = sq.QuestionId,
                                       QuestionText = sq.Question.QuestionText,
                                       LastAnswer = sq.Question.Answers
                                           .Where(a => a.UserId == userId)
                                           .OrderByDescending(a => a.AnswerId)
                                           .Select(a => a.AnswerText)
                                           .FirstOrDefault()
                                   };
    

    This should be only one single database query because now it’s LINQ to Entities with IQueryable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given the task to design a database to store a lot
I have three types of questions: Vocab (represented by question:answer pairs) Grammar (represented by
This question is related to this post: SQL design for survey with answers of
Just wanted opinions on a design question. If you have a C++ class than
I'm building a database that will be used to store Questions and Answers. There
I have a design issue that I would appreciate some input on. I would
I have started design of a ColdFusion application that is entirely web based. Not
I have to design a data structure that is to be used in a
I have this design which I cannot seem to get right, I would like
I have to design a form with an input inside it. I use background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.