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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:08:05+00:00 2026-05-20T05:08:05+00:00

i have a list of project objects: IEnumerable<Project> projects a Project class as a

  • 0

i have a list of project objects:

IEnumerable<Project> projects

a Project class as a property called Tags. this is a int[]

i have a variable called filteredTags which is also a int[].

So lets say my filtered tags variable looks like this:

 int[] filteredTags = new int[]{1, 3};

I want to filter my list (projects) to only return projects that have ALL of the tags listed in the filter (in this case at least tag 1 AND tag 3 in the Tags property).

I was trying to use Where() and Contains() but that only seems to work if i am comparing against a single value. How would i do this to compare a list against another list where i need a match on all the items in the filtered list ??

  • 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-05-20T05:08:06+00:00Added an answer on May 20, 2026 at 5:08 am

    EDIT: better yet, do it like that:

    var filteredProjects = 
        projects.Where(p => filteredTags.All(tag => p.Tags.Contains(tag)));
    

    EDIT2: Honestly, I don’t know which one is better, so if performance is not critical, choose the one you think is more readable. If it is, you’ll have to benchmark it somehow.


    Probably Intersect is the way to go:

    void Main()
    {
        var projects = new List<Project>();
        projects.Add(new Project { Name = "Project1", Tags = new int[] { 2, 5, 3, 1 } });
        projects.Add(new Project { Name = "Project2", Tags = new int[] { 1, 4, 7 } });
        projects.Add(new Project { Name = "Project3", Tags = new int[] { 1, 7, 12, 3 } });
    
        var filteredTags = new int []{ 1, 3 };
        var filteredProjects = projects.Where(p => p.Tags.Intersect(filteredTags).Count() == filteredTags.Length);  
    }
    
    
    class Project {
        public string Name;
        public int[] Tags;
    }
    

    Although that seems a little ugly at first. You may first apply Distinct to filteredTags if you aren’t sure whether they are all unique in the list, otherwise the counts comparison won’t work as expected.

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

Sidebar

Related Questions

So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id
I have a List<int> and a List<customObject> . The customObject class has an ID
The Problem: I have a forms project which instantiates a class that is defined
I have a method which should return a list of anonymous objects with a
Say I have a list of all Projects , and that I group them
I have a List of Foo. Foo has a string property named Bar. I'd
I have a list, each item of which has several things in it, including
I have a list of objects I wish to sort based on a field
I have a large project which was originally written in C#1.1 and is now
I have found this example on StackOverflow: var people = new List<Person> { new

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.