实体框架代码优先 - 获取具有特定标签的博客文章

时间:2012-08-29 23:57:27

标签: c# linq entity-framework tsql ef-code-first

我在这里遗漏了一些东西,但我不确定那是什么。所以我需要一双眼睛。我想获得与其相关的某些标签的博客帖子。我的模型结构如下所示:

public partial class BlogPost : IEntity {

    public BlogPost() {

        this.BlogPostComments = new HashSet<BlogPostComment>();
        this.BlogPostUrls = new HashSet<BlogPostUrl>();
        this.TagsForBlogPosts = new HashSet<TagsForBlogPost>();
    }

    public System.Guid Key { get; set; }
    public System.Guid LanguageKey { get; set; }
    public Nullable<int> SecondaryKey { get; set; }
    public string Title { get; set; }

    //...

    public virtual ICollection<BlogPostComment> BlogPostComments { get; set; }
    public virtual Language Language { get; set; }
    public virtual ICollection<BlogPostUrl> BlogPostUrls { get; set; }
    public virtual ICollection<TagsForBlogPost> TagsForBlogPosts { get; set; }
}

public partial class Tag : IEntity {

    public Tag() {

        this.TagsForBlogPosts = new HashSet<TagsForBlogPost>();
        this.TagsForDynamicPages = new HashSet<TagsForDynamicPage>();
    }

    public System.Guid Key { get; set; }
    public System.Guid LanguageKey { get; set; }
    public string TagName { get; set; }

    //...    

    public virtual Language Language { get; set; }
    public virtual ICollection<TagsForBlogPost> TagsForBlogPosts { get; set; }
    public virtual ICollection<TagsForDynamicPage> TagsForDynamicPages { get; set; }
}

public partial class TagsForBlogPost : IEntity {

    public System.Guid Key { get; set; }
    public System.Guid BlogPostKey { get; set; }
    public System.Guid TagKey { get; set; }

    public virtual BlogPost BlogPost { get; set; }
    public virtual Tag Tag { get; set; }
}

作为输入,我有string[]个标签,我需要博客文章。现在,我可以使用以下代码:

public void Get(string[] tags) {

    var posts = dbConxtext.BlogPosts.Where(x => 
        x.TagsForBlogPosts.Any(y => tags.Contains(y.Tag.TagName)));

    //

}

但是它产生的IN子句不是我想要的。这是上面代码生成的T-SQL:

SELECT 
[Extent1].[Key] AS [Key], 
[Extent1].[LanguageKey] AS [LanguageKey], 
[Extent1].[SecondaryKey] AS [SecondaryKey], 
[Extent1].[Title] AS [Title], 
[Extent1].[BriefInfo] AS [BriefInfo], 
[Extent1].[Content] AS [Content], 
[Extent1].[ImagePath] AS [ImagePath], 
[Extent1].[IsApproved] AS [IsApproved], 
[Extent1].[CreationIp] AS [CreationIp], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[LastUpdateIp] AS [LastUpdateIp], 
[Extent1].[LastUpdatedOn] AS [LastUpdatedOn]
FROM [dbo].[BlogPosts] AS [Extent1]
WHERE  EXISTS (SELECT 
    1 AS [C1]
    FROM  [dbo].[TagsForBlogPosts] AS [Extent2]
    INNER JOIN [dbo].[Tags] AS [Extent3] ON [Extent2].[TagKey] = [Extent3].[Key]
    WHERE ([Extent1].[Key] = [Extent2].[BlogPostKey]) AND ([Extent3].[TagName] IN (N'nuget',N'mvc'))
)

我想要的是与这些标签完全匹配。知道如何实现这个目标吗?

修改

我需要的是:

帖子A有 tag1 tag2 tag3 。帖子B有 tag1 tag3 。如果字符串数组包含 tag1 tag2 ,则只应选择帖子A,因为它同时包含 tag1 tag2

答案:

感谢@msarchet,我设法让它工作了。这是LINQ查询:

public void Get(string[] tags) {

    var posts = dbConxtext.BlogPosts
        .Where(x => tags.All(t => x.TagsForBlogPosts.Any(y => y.Tag.TagName == t)));

    //

}

并生成了T-SQL:

SELECT 
[Extent1].[Key] AS [Key], 
[Extent1].[LanguageKey] AS [LanguageKey], 
[Extent1].[SecondaryKey] AS [SecondaryKey], 
[Extent1].[Title] AS [Title], 
[Extent1].[BriefInfo] AS [BriefInfo], 
[Extent1].[Content] AS [Content], 
[Extent1].[ImagePath] AS [ImagePath], 
[Extent1].[IsApproved] AS [IsApproved], 
[Extent1].[CreationIp] AS [CreationIp], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[LastUpdateIp] AS [LastUpdateIp], 
[Extent1].[LastUpdatedOn] AS [LastUpdatedOn]
FROM [dbo].[BlogPosts] AS [Extent1]
WHERE  NOT EXISTS (SELECT 
    1 AS [C1]
    FROM  (SELECT 
        N'nuget' AS [C1]
        FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
    UNION ALL
        SELECT 
        N'razor' AS [C1]
        FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]
    WHERE ( NOT EXISTS (SELECT 
        1 AS [C1]
        FROM  [dbo].[TagsForBlogPosts] AS [Extent2]
        INNER JOIN [dbo].[Tags] AS [Extent3] ON [Extent2].[TagKey] = [Extent3].[Key]
        WHERE ([Extent1].[Key] = [Extent2].[BlogPostKey]) AND ([Extent3].[TagName] = [UnionAll1].[C1])
    )) OR (CASE WHEN ( EXISTS (SELECT 
        1 AS [C1]
        FROM  [dbo].[TagsForBlogPosts] AS [Extent4]
        INNER JOIN [dbo].[Tags] AS [Extent5] ON [Extent4].[TagKey] = [Extent5].[Key]
        WHERE ([Extent1].[Key] = [Extent4].[BlogPostKey]) AND ([Extent5].[TagName] = [UnionAll1].[C1])
    )) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT 
        1 AS [C1]
        FROM  [dbo].[TagsForBlogPosts] AS [Extent6]
        INNER JOIN [dbo].[Tags] AS [Extent7] ON [Extent6].[TagKey] = [Extent7].[Key]
        WHERE ([Extent1].[Key] = [Extent6].[BlogPostKey]) AND ([Extent7].[TagName] = [UnionAll1].[C1])
    )) THEN cast(0 as bit) END IS NULL)
)

1 个答案:

答案 0 :(得分:3)

那么你试图按照你的评论做什么

  

是肯定的。假设:帖子A有tag1,tag2和tag3。帖子B有tag1,tag3。如果字符串数组包含tag1和tag2,则应选择Post A.

var posts = dbConxtext.BlogPosts.Where(x => tags.All(t => 
  x.TagsForBlogPost.Any(y => y.Tag.TagName == t)));

因此,请确保tags

中包含所有TagsForBlogPost

警告,这可能会产生可怕的SQL