2011-11-07 45 views
4

我有這樣嵌套其中的查詢在實體框架

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

回答

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

謝謝fabiano ...你知道任何良好的教程建設這樣的拉姆達表達式 –

+0

我會閱讀MSDN文章http://msdn.microsoft.com/en-us/library/bb397926.aspx並採取查看可用的擴展方法http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods.aspx。或者只是谷歌的一些教程。還要注意並非所有LINQ擴展在Linq to Entities中都可用:http://msdn.microsoft.com/en-us/library/bb738550.aspx – Fabiano