One of the slower elements of a database conversation is the transfer of the selected data from the database management system to your process. It is wise not to send any more data to your process than you actually plan to use.
Apparently you have a sequence of Questions
where every Question
has zero or more QuestionOptions
, and every QuestionOption
belongs to exactly one Question
, namely the Question
with the Id
equal to the QuestionOption.QuestionId
. A simple one-to-many with a foreign key in QuestionId
.
If you would fetch the Question
with Id == 4 with its ten thousand QuestionOptions
, you know that every QuestionOption
will have a QuestionId
that has a value of 4. You will be transferring the value 4 a 10 thousand times, while you'll probably wouldn't even use it, as you already know it equals the Question.Id
.
The solutions is: only use Include if you plan to update a database item.
In all other cases use Select.
Select only the properties you actually plan to use.
In Method syntax (or use a similar query syntax if you like)
var result = context.Questions.Join(context.QuestionOptions, // join Questions and QuestionOptions
question => question.Id, // from every Question take the Id
questionOption => questionOption.QuestionId, // from every Option take the QuestionId
(question, questionOption) => new // when they match make a new object
{ // Select only the properties you plan to use:
Question = new
{
Id = question.Id,
... other question properties
},
Option = new
{
Id = questionOption.Id,
// not needed: questionOption.QuestionId, it equals Question.Id
... other properties you plan to use
}
});
Only do ToList
if you really plan to use all fetched elements. Keep your result an IQueryable as long as possible.
If you rather have each Question with its QuestionOptions
, consider using Queryable.GroupBy
var questionsWithTheirOptions = context.Questions
.GroupJoin(context.QuestionOptions, // GroupJoin Questions and QuestionOptions
question => question.Id, // from every Question take the Id
questionOption => questionOption.QuestionId, // from every Option take the QuestionId
(question, optionsOfQuestion) => new // when they match make a new object
{ // desired Question Properties
Id = question.Id,
...
// The options of this question:
Options = optionsOfQuestion.Select(option => new
{
Id = questionOption.Id,
// not needed: questionOption.QuestionId, it equals Question.Id
...
})
.ToList()
});