嵌套在Entity Framework中的查询

时间:2011-11-07 08:16:12

标签: linq entity-framework

我有一个这样的课程

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; }
    }

和像这样的问题类

 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; }

    }

我想编写一个查询来选择包含特定问题的调查

沿着这些方向的东西

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

1 个答案:

答案 0 :(得分:4)

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