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

  • Home
  • SEARCH
  • 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 7919155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:46:07+00:00 2026-06-03T15:46:07+00:00

Having brain-lock and need a simple/quick answer – I am probably looking at this

  • 0

Having brain-lock and need a simple/quick answer – I am probably looking at this from the wrong end, but I have a lot on my plate this week. 🙁

Using linq-to-sql, I have a table with items mapped to many categories via a mapping table.

I now need to pull out all items which are mapped to multiple categories, but only items which are mapped to ALL the categories required. This is a change to the original brief which was to pull data which is mapped to any category id.

Example:

person A is mapped to category 1, category 2 and catgory 3
person B is mapped to category 1 and catgory 3

So if I select category 1 & 2 both person items are returned, however if I selected 1, 2 & 3 only person A should be returned.

Here is the original (simplified) code which returns ANY item:

IList<Person> results = (from a in data from b in a.CategoryMappings 
                   where catIDs.AsEnumerable().Contains(b.CategoryID) 
                   select a).Distinct().ToList();

catIDs is an IList<int> taken from the data sent to the query, this can be of any length. And data is either an IQueryable.

Any idea how I can build this query?

T

  • 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-03T15:46:09+00:00Added an answer on June 3, 2026 at 3:46 pm

    Howsaboutthis? Mudu beat me to it with the exact same solution, so this is my alternative.

    Only select objects from data if all the elements in the catIDs list satisfy the condition, that p.CategoryMappings must contain the element.

    var results = from p in data 
                  where catIds.All(i => p.CategoryMappings.Contains(i))
                  select p;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is probably a really simple question but I'm having a brain
Greetings, Might be having a brain fart this morning, but I need a way
I'm seriously having a brain fart here, but I can't figure out why this
I am having a bit of a brain fart. I need to slice this
I'm probably just having a brain fart, but I have 2 library assembly projects
I think I am having a brain fart. This sould be a very simple
I have done this before but seem to be having a brain freeze and
I know this should be simple however I'm having a bit of a brain
Having a brain freeze on how best to approach this, I have a ton
I want to localize a presentation and I'm having a brain freeze on this

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.