简体   繁体   English

连接表中的LINQ查询

[英]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. 所以例如,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: 如果您在Question实体中有类别的导航属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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