我该如何编写这个查询实体框架

时间:2015-12-07 18:00:18

标签: c# sql entity-framework

enter image description here

如何在CS中写这个应该用??替换****

MSSQL查询:

SELECT        *FROM            Advertisement INNER JOIN
                     SubCategory ON Advertisement.SubCategoryID = SubCategory.SubCategoryID INNER JOIN
                     CategoryHaveSubCategory ON SubCategory.SubCategoryID = CategoryHaveSubCategory.SubCategoryID INNER JOIN
                     Category ON CategoryHaveSubCategory.CategoryID = Category.CategoryID WHERE        (CategoryHaveSubCategory.CategoryID = 1)

C#查询:

query = (from a in db.Advertisements
                 orderby a.Date descending
                 where **** && a.Deleted == false
                 select a).Skip(skip).Take(PAGE_SIZE);

2 个答案:

答案 0 :(得分:0)

您没有包含Advertisement实体的重要部分 - 导航属性。从显示的关系我假设你有这样的东西

class Advertisement
{
    // ...
    public SubCategory SubCategory { get; set; }
}

那么你要求的条件应该是这样的

where a.SubCategory.Categories.Any(c => c.CategoryID == 1) && a.Deleted == false

答案 1 :(得分:0)

   query = (from a in db.Advertisements
                 orderby a.Date descending
                 where a.SubCategory.Categories.Any(c => c.CategoryID == 1) 
                 && a.Deleted == false
                 select a).Skip(skip).Take(PAGE_SIZE);