6

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"));

1 Answer 1

8
Survey s1 = db.Surveys
    .Where(s => s.Questions.Any(q => q.QuestionId == "1eb56610-853d-4a9e-adc7-e0ec069390b7"))
    .FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you fabiano...do u know any good tutorial for building such lamda expressions
I would read the msdn articles msdn.microsoft.com/en-us/library/bb397926.aspx and take a look at the available extension methods msdn.microsoft.com/en-us/library/…. Or just google for some tutorials. also note that not all LINQ extensions are available in Linq to Entities: msdn.microsoft.com/en-us/library/bb738550.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.