2011-11-07 49 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