我在这里遗漏了一些东西,但我不确定那是什么。所以我需要一双眼睛。我想获取与它相关联的某些标签的博客文章。我的模型结构如下所示:
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,则只应选择 Post 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)
)