Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested where query 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 a 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 which contains a particular question

something along these lines

 Survey s1 = db.Surveys.Where(s => s.Questions.Where(q => q.QuestionId == "1eb56610-853d-4a9e-adc7-e0ec069390b7"));
like image 330
taher chhabrawala Avatar asked Nov 07 '11 08:11

taher chhabrawala


1 Answers

Survey s1 = db.Surveys
    .Where(s => s.Questions.Any(q => q.QuestionId == "1eb56610-853d-4a9e-adc7-e0ec069390b7"))
    .FirstOrDefault();
like image 193
Fabiano Avatar answered Sep 22 '22 10:09

Fabiano