简体   繁体   中英

LINQ Query in Junction Table

I have a table that looks like this :-

CategoryId | QuestionId
----------------------- 
1      |      2 
1      |      3 
3      |      2 
4      |      3

I need to pull out all of the Questions that are not in use by a specific category. so for eg, CategoryID = 1.

The result should be that there are no questions to display.

Anyone know the best way to do this? so far i've not got anywhere with it.

EDIT **

public partial class FAQ
    {
        public FAQ()
        {
            this.FAQCategoriesFAQs = new HashSet<FAQCategoriesFAQ>();
        }

        public int Id { get; set; }
        public string Question { get; set; }
        public string Answer { get; set; }
        public bool IsVisible { get; set; }
        public Nullable<System.DateTime> DateLastUpdated { get; set; }
        public System.DateTime DateCreated { get; set; }
        public bool IsDeleted { get; set; }

        public virtual ICollection<FAQCategoriesFAQ> FAQCategoriesFAQs { get; set; }
    }

 public partial class FAQCategory
    {
        public FAQCategory()
        {
            this.FAQCategoriesFAQs = new HashSet<FAQCategoriesFAQ>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public int DomainId { get; set; }
        public Nullable<System.DateTime> DateLastUpdated { get; set; }
        public System.DateTime DateCreated { get; set; }
        public bool IsDeleted { get; set; }
        public int SortOrder { get; set; }

        public virtual Domain Domain { get; set; }
        public virtual ICollection<FAQCategoriesFAQ> FAQCategoriesFAQs { get; set; }
    }

 public partial class FAQCategoriesFAQ
    {
        public int FAQCategoryId { get; set; }
        public int FAQId { get; set; }
        public int SortOrder { get; set; }

        public virtual FAQCategory FAQCategory { get; set; }
        public virtual FAQ FAQ { get; set; }
    }

If you have navigation property for categories in Question entity:

db.Questions.Where(q => !q.Categories.Any(c => c.Id == id))

UPDATE Thus your junction table differs a little bit from your original question :)

db.FAQs.Where(q => !q.FAQCategoriesFAQs.Any(qc => qc.FAQCategoryId == id))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM