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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:41:39+00:00 2026-05-26T07:41:39+00:00

Looking for an example where I can filter my collection based on some filtering

  • 0

Looking for an example where I can filter my collection based on some filtering criteria.

I have been looking for some example where given a list /array i can filter a collection.

In the example below in my find method I am trying to filter based on 2 values ,looking for something like an “IN” function any suggestions?

        class Program
        {
            static void Main()
            {
                //Print all customres that belong to below deparments and match on surname
                var criteria=new Criteria
                                 {
                                     Departments = new List<string> {"BusinessAnalyst", "Account"},
                                     Surname = "Bloggs"
                                 };

                List<Customer> customers = Repository.Find(criteria);

                customers.ForEach(x => Console.WriteLine(string.Format("Surname: {0} Department :{1}", x.Surname,x.Department)));

                Console.Read();
            }
        }

        public class Repository
        {
           public static List<Customer>GetCustomers()
            {
                return  new List<Customer>
                                    {
                                        new Customer { Name = "Jon",Surname="Smith",Department = DepartmentType.Managers},
                                        new Customer{Name = "Bill",Surname = "Gates",Department = DepartmentType.Managers},
                                        new Customer { Name = "Mary",Surname = "Bug",Department = DepartmentType.Developers},
                                        new Customer { Name = "Mark",Surname="Boo",Department = DepartmentType.Account},
                                        new Customer{Name = "Ron",Surname = "Scott",Department = DepartmentType.Managers},
                                        new Customer { Name = "Jonny",Surname = "Dip",Department = DepartmentType.Developers},
                                        new Customer { Name = "Mary",Surname = "Bloggs",Department = DepartmentType.BusinessAnalyst},

                                        new Customer { Name = "Mary",Surname = "Bug",Department = DepartmentType.Account},
                                        new Customer { Name = "Jonny",Surname = "Dip",Department = DepartmentType.Account},
                                        new Customer { Name = "Mary",Surname = "Bloggs",Department = DepartmentType.Managers}
                                    };
            }

           public static List<Customer> Find(Criteria criteria)
           {
               List<Customer>customers=Repository.GetCustomers();

               //Filter on departments
               //ERROR HERE AS I cannot do this "IN" would be fantastic.
               customers = customers.Contains(criteria.Departments);

               //now filter on name
               customers = customers.Where(x => x.Surname == criteria.Surname).ToList();


               return customers;
           }
        }

        public enum DepartmentType
        {
            Account,
            Managers,
            Developers,
            BusinessAnalyst
        }
        public class Customer
        {
            public string Name { get; set; }
            public string Surname { get; set; }
            public DepartmentType Department { get; set; }

        }
        public class Criteria
        {
            public Criteria()
            {
                Departments=new List<string>();
            }
            public string Name { get; set; }
            public string Surname { get; set; }
            public List<string> Departments { get; set; }
        }
  • 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-26T07:41:40+00:00Added an answer on May 26, 2026 at 7:41 am
    public static List<Customer> Find(Criteria criteria)
    {
        List<Customer> customers = Repository.GetCustomers();
    
        var customers2 = customers.Where(x => criteria.Departments.Contains(x.Department.ToString()));
    
        var customers3 = customers2.Where(x => x.Surname == criteria.Surname);
    
        return customers3.ToList();
    }
    

    But considering you use an enum for the Department (DepartmentType), shouldn’t your Criteria class use the same instead of a string?

    If you define the criteria.Departments as List<DepartmentType> then you can write

    public static List<Customer> Find(Criteria criteria)
    {
        List<Customer> customers = Repository.GetCustomers();
    
        var customers2 = customers.Where(x => criteria.Departments.Contains(x.Department));
    
        var customers3 = customers2.Where(x => x.Surname == criteria.Surname);
    
        return customers3.ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list that the user can filter in several ways. two of
I have been looking for some examples and solutions using the jquery-datatables-editable plugin and
Looking at this jQuery example, how can I modify the code so that it
Looking at examples about socket programming, we can see that some people use AF_INET
I am looking for some examples of a .bat OR .wsh script that can
I'm looking for example of how I would solve the scenario below: Imagine my
I'm looking at adding some basic search and filtering functionality in a generic/standardized way
I'm looking for example code for setting up a connection to a bluetooth device
I'm trying to learn Rails better by looking at example applications, and while looking
Looking for an example that: Launches an EXE Waits for the EXE to finish.

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.