none
Join many-to-many in Code First? RRS feed

  • Question

  • I'm using Code First. I have the following two POCOs:

    public class Question
    {
    		public Question()
    		{
    		 	Keywords = new HashSet<Keyword>();
    		}
    
    		[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    		public int QuestionId { get; set; }
    
    		public string QuestionText { get; set; }
    		public string PhotoUrl { get; set; }
    		public int SortOrder { get; set; }
    		public string PdfUrl { get; set; }
    		public string VideoUrl { get; set; }
    
    		public ICollection<Keyword> Keywords { get; set; }
    }
    
    public class Keyword
    {
    		public Keyword()
    		{
    			 Questions = new HashSet<Question>();
    		}
    
    		[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    		public int KeywordId { get; set; }
    
    		public string Description { get; set; }
    		public ICollection<Question> Questions { get; set; }
    }
    


    I want to make a query, to return all the Questions that have a certain keyword associated with it.

    Here's my attempt. It returns results, but returns way more results than it should:

    var questions = from q in db.Questions
    		from k in db.Keywords
    		where k.Description == "Fit" /// keywordSingle
    		select q;
    
    



    Thursday, July 14, 2011 6:09 PM

Answers

All replies