Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:54:31+00:00 2026-06-10T11:54:31+00:00

I a missing something here but I am not sure what is that. So

  • 0

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)
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:54:33+00:00Added an answer on June 10, 2026 at 11:54 am

    So what your trying to do per your comment

    yes. Assuming: the Post A has tag1, tag2 and tag3. The post B has tag1, tag3. If the string array includes tag1 and tag2, the Post A should be selected.

    Is

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

    So make sure that All of the tags are contained in TagsForBlogPost

    Warning, this may produce terrible SQL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im sure I am missing something here but none the less. foo['bar'] = nil
I am missing something obvious, but I'm not sure what. I have a single
I am missing something obvious, but I'm not sure what. I have a single
I might be missing something here but am unable to understand the true purpose
I'm probably missing something obvious here but here's what I'm trying to do. From
I have to be missing something simple here but it escapes me. After the
I think i must be missing something simple here but I can't figure out
I think I'm just missing something simple here but here is what I am
Must be missing something really obvious here but how do you change the size
Probably missing something completely obvious here, but here goes. I'm starting out with Spring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.