连接表中的LINQ查询

时间:2013-12-03 09:08:43

标签: c# linq

我有一张如下表: -

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

我需要提取所有未被特定类别使用的问题。所以例如,CategoryID = 1。

结果应该是没有问题要显示。

任何人都知道最好的方法吗?到目前为止,我没有任何地方。

编辑**

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; }
    }

1 个答案:

答案 0 :(得分:2)

如果您在Question实体中有类别的导航属性:

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

更新因此,您的联结表与原始问题略有不同:)

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