且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查询使用LINQ /实体框架一个多一对多的关系。 CodeFirst

更新时间:2022-12-26 20:50:43

您可以这样做:

var cat_id=1; // Change this variable for your real cat_id

var query= from article in db.Articles
           where article.Categories.Any(c=>c.Category_ID==cat_id)
           select article;



这样你会得到一个满足您需要的条件的条款。这是由该查询生成的SQL代码:

This way you will get the articles that satisfies the condition you want. This is the sql code that is generated by that query:

    SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Title] AS [Title]
    FROM [dbo].[Articles] AS [Extent1]
    WHERE  EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[ArticleCategories] AS [Extent2]
        WHERE ([Extent1].[Id] = [Extent2].[Article_Id]) AND ([Extent2].[Category_Id] = @p__linq__0))