I wrote a linq query to fetch multiple nested entities. I need to get all the questions except the ones that are deleted and the ones with a parentId
. This is where it's going wrong. All the results are fine, except the result set still contains questions where the parent id is not null. What am I doing wrong?
I think it might has to do with the filterquestions
, these are also questions self referring to another question. I need these questions only in the list of filterquestions
and not in both.
public async Task<QuestionList?> GetAsyncQuestionTree(Guid id)
{
var tree = await _dbContext.QuestionLists
.Where(list => list.Id == id)
.Include(list => list.Modules)!
.ThenInclude(module => module.Questions!.Where(m => m.isDeleted == false && m.ParentId == null))!
.ThenInclude(question => question.AnswerOptions)
.Include(list => list.Modules)!
.ThenInclude(module => module.Questions!.Where(m => m.isDeleted == false && m.ParentId == null))!
.ThenInclude(question => question.FilterQuestions)
.ThenInclude(question => question.AnswerOptions)
.FirstOrDefaultAsync();
return tree;
}