Madhav Sharma

Madhav Sharma

  • 741
  • 892
  • 36.7k

Getting error "Lambda expression used inside Include is not"

May 30 2020 11:34 AM
Once I writting quary
  1. var testresult = await _unit.Context.Tbltest.Include(p => p.Tblquestion).  
  2. ThenInclude(post => post.Tblquestionoptions.Where(k => k.Id == id)).Where(t => t.Id == id).FirstOrDefaultAsync();  
  3. It throw exception "Lambda expression used inside Include is not"  
  4. Full Code :  
  5. public async Task<ApiResult<TestQueryModel>> GetQuestionOptionByIdAsync(long id)  
  6. {  
  7. TestQueryModel modeltest = null;  
  8. try  
  9. {  
  10. //var testresult = await _unit.Context.Tbltest.Include(blog => blog.Tblquestion)  
  11. //.ThenInclude(post => post.Tblquestionoptions).Where(t => t.Id == testId).FirstOrDefaultAsync();  
  12. //var testresult1 = await _unit.Context.Tblquestion.Include(post => post.Tblquestionoptions).  
  13. // Where(t => t.Id == id).FirstOrDefaultAsync();  
  14. var testresult = await _unit.Context.Tbltest.Include(p => p.Tblquestion).  
  15. ThenInclude(post => post.Tblquestionoptions.Where(k => k.Id == id)).Where(t => t.Id == id).FirstOrDefaultAsync();  
  16. if (testresult != null)  
  17. {  
  18. modeltest = new TestQueryModel();  
  19. modeltest.UserId = CurrentUserInfo.CurrentUser;  
  20. modeltest.TestId = testresult.Id;  
  21. modeltest.GradeId = testresult.GradeId;  
  22. modeltest.SubjectId = testresult.SubjectId;  
  23. modeltest.TopicId = testresult.TopicId;  
  24. modeltest.Questions = testresult.Tblquestion.Select(q => new TestQuestionQueryModel  
  25. {  
  26. Question = q.Name,  
  27. QuestionId = q.Id,  
  28. Options = q.Tblquestionoptions.Select(t => new TestQuestionOptionsQueryModel  
  29. {  
  30. OptionId = t.Id,  
  31. Option = t.Name,  
  32. IsSelect = t.IsMatched.HasValue ? t.IsMatched.Value : false  
  33. }).ToList()  
  34. }).ToList();  
  35. return new ApiResult<TestQueryModel>(new ApiResultCode(ApiResultType.Success), modeltest);  
  36. }  
  37. return new ApiResult<TestQueryModel>(new ApiResultCode(ApiResultType.Error, messageText: "no data found"));  
  38. }  
  39. catch (Exception ex)  
  40. {  
  41. ErrorTrace.Logger(LogArea.RepositoryLayer, ex);  
  42. return new ApiResult<TestQueryModel>(new ApiResultCode(ApiResultType.Error, messageText: "Error while geting data"));  
  43. }  
  44. }  

Answers (2)