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 7910777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:59:49+00:00 2026-06-03T12:59:49+00:00

I use many-to-many relationship for my tables. There is a query: var query =

  • 0

I use many-to-many relationship for my tables.

There is a query:

var query = from post in context.Posts
        from tag in post.Tags where tag.TagId == 10
        select post;

Ok, it works fine. I get posts having the tag specified by id.

I have a collection of tag ids. And i want to get posts having every tag in my collection.

I try the following way:

var tagIds = new int[]{1, 3, 7, 23, 56};

var query = from post in context.Posts
        from tag in post.Tags where tagIds.Contains( tag.TagId )
        select post;

It doesn’t work. The query returns all posts having ANY one of the specified tags.

I want to get a clause like this but dynamicaly for any count of tags in the collection:

post.Tags.Whare(x => x.TagId = 1 && x.TagId = 3 && x.TagId = 7 && ... )
  • 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-03T12:59:51+00:00Added an answer on June 3, 2026 at 12:59 pm

    You shouldn’t project each post’s tags in the outer query; rather, you need to use an inner query which performs the check for the outer filter. (In SQL, we used to call it a correlated subquery.)

    var query = 
        from post in context.Posts
        where post.Tags.All(tag => tagIds.Contains(tag.TagId))
        select post;
    

    Alternate syntax:

    var query = 
        context.Posts.Where(post =>
            post.Tags.All(tag => 
                tagIds.Contains(tag.TagId)));
    

    Edit: Correcting per Slauma’s clarification. The version below returns posts which contain, at least, all the tags in the tagIds collection.

    var query = 
        from post in context.Posts
        where tagIds.All(requiredId => post.Tags.Any(tag => tag.TagId == requiredId))
        select post;
    

    Alternate syntax:

    var query = 
        context.Posts.Where(post => 
            tagIds.All(requiredId => 
                post.Tags.Any(tag =>
                    tag.TagId == requiredId)));
    

    Edit2: Corrected above per Slauma. Also including another alternative making full use of query syntax below:

    // Project posts from context for which
    // no Ids from tagIds are not matched
    // by any tags from post
    var query =
        from post in context.Posts
        where
        ( 
            // Project Ids from tagIds that are
            // not matched by any tags from post
            from requiredId in tagIds
            where
            (
                // Project tags from post that match requiredId
                from tag in post.Tags
                where tag.TagId == requiredId
                select tag
            ).Any() == false
            select requiredId 
        ).Any() == false
        select post;
    

    I’ve used .Any() == false to simulate the NOT EXISTS operator in Transact-SQL.

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

Sidebar

Related Questions

I have two tables books and tags which are in a many-to-many relationship via
There are two tables, which have a relationship(one-to-many). There are Meals table which contains
I have a many-to-many relationship in my django application where I use the add
Original Query I'm trying to rewrite: SELECT Table1.* FROM Table1 INNER JOIN Table2 ON
We have two tables, ActivityForm and Field which are given a many-to-many relationship via
I have two tables with a many to one relationship which represent lots and
I have a simple many to many relationship set up between tasks and tags.
I have a many-to-many table relationship in MySQL involving three tables: tickets , ticket_solutions
Here's my problem: I have three tables that represent a many to many relationship
Here I have another query bothering me. Select S.ID,S.Name,S.Surname,B.Title,SB.DateAndTimeIssued FROM Students S INNER JOIN

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.