Subquery in Entity Framework

I have a class like this

public class Survey { public Survey() { SurveyResponses=new List<SurveyResponse>(); } public Guid SurveyId { get; set; } public string SurveyName { get; set; } public string SurveyDescription { get; set; } public virtual ICollection<Question> Questions { get; set; } public virtual ICollection<SurveyResponse> SurveyResponses { get; set; } } 

and Question class like this

  public class Question { public Guid QuestionId { get; set; } public string QuestionText { get; set; } public QuestionType QuestionType { get; set; } public virtual ICollection<Option> Options { get; set; } public bool IsOtherAllowed { get; set; } public virtual ICollection<Answer> Answers { get; set; } } 

I want to write a query to select a survey containing a specific question

something along these lines

  Survey s1 = db.Surveys.Where(s => s.Questions.Where(q => q.QuestionId == "1eb56610-853d-4a9e-adc7-e0ec069390b7")); 
+4
source share
1 answer
 Survey s1 = db.Surveys .Where(s => s.Questions.Any(q => q.QuestionId == "1eb56610-853d-4a9e-adc7-e0ec069390b7")) .FirstOrDefault(); 
+4
source

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


All Articles