I a missing something here but I am not sure what is that. So I need a pair of eyes. I want to get blog posts which has certain tags associated with it. My model structure looks like as below:
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; }
}
As input I have string[] tags which I need for a blog posts to have. For now, I can have the following code working:
public void Get(string[] tags) {
var posts = dbConxtext.BlogPosts.Where(x =>
x.TagsForBlogPosts.Any(y => tags.Contains(y.Tag.TagName)));
//
}
But it generates an IN Clause which is not what I want. Here is the T-SQL which the above code generates:
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'))
)
What I want is to exact match to those tags. Any idea how I can achieve that?
Edit:
What I need is as below:
The Post A has tag1, tag2 and tag3. The post B has tag1, tag3. If the string array includes tag1 and tag2, only the Post A should be selected because it has both tag1 and tag2.
Answer:
I managed to get it working thanks to @msarchet. Here is the LINQ query:
public void Get(string[] tags) {
var posts = dbConxtext.BlogPosts
.Where(x => tags.All(t => x.TagsForBlogPosts.Any(y => y.Tag.TagName == t)));
//
}
And generated 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)
)
So what your trying to do per your comment
Is
So make sure that All of the
tagsare contained inTagsForBlogPostWarning, this may produce terrible SQL