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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:13:49+00:00 2026-05-25T16:13:49+00:00

Wondering what is the best approach to filter an in memory list dynamically. Lets

  • 0

Wondering what is the best approach to filter an in memory list dynamically.

Lets suppose I have in memory List and I need to write a find that filters the in memoryList based on the criteria.
Obviously if a value is null or empty do use it.
How do you build the predicate. I have looked at the predicateBuilder but not sure how to use it.

See code below

    public class CustomerService
{
    private IEnumerable<Customer> customersInMemoryAlready;

    public CustomerService()
    {
        customersInMemoryAlready = new List<Customer>();//Pretend we have fetched 200 customers here
    }
     public IEnumerable<Customer> Find(Criteria criteria)
     {
         IEnumerable<Customer> results = GetInMemoryCustomers();

         //Now filter based on criteria How would you do it

         if(!string.IsNullOrEmpty(criteria.Surname))
         {
             //?
         }
         if (!criteria.StartDate.HasValue &&  !criteria.EndDate.HasValue)
         {
             //?Get all dateOfBirth between StartDate and EnDate
         }

         return results;
     }

    private IEnumerable<Customer> GetInMemoryCustomers()
    {
        return customersInMemoryAlready;
    }
}
public class Customer
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public DateTime?DateOfBirth { get; set; }
    public string City { get; set; }
}

public class Criteria
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public DateTime?StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string City { get; set; } 
}

Any suggestions?
Thanks

  • 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-25T16:13:50+00:00Added an answer on May 25, 2026 at 4:13 pm

    Sure, you just use Where:

     public IEnumerable<Customer> Find(Criteria criteria)
     {
         IEnumerable<Customer> results = GetInMemoryCustomers();
    
         //Now filter based on criteria How would you do it
    
         if(!string.IsNullOrEmpty(criteria.Surname))
         {
             results = results.Where(c => c.Surname == criteria.Surname);
         }
         if (criteria.StartDate.HasValue && criteria.EndDate.HasValue)
         {
             DateTime start = criteria.StartDate.Value;
             DateTime end = criteria.EndDate.Value;
             results = results.Where(c => c.DateOfBirth != null &&
                                          c.DateOfBirth.Value >= start &&
                                          c.DateOfBirth.Value < end);
         }
    
         return results;
    }
    

    EDIT: You can use the lifted operators for DateTime? if you want to here:

     if (criteria.StartDate.HasValue && criteria.EndDate.HasValue)
     {
         results = results.Where(c => c.DateOfBirth != null &&
                                      c.DateOfBirth >= criteria.StartDate &&
                                      c.DateOfBirth < criteria.EndDate);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering what the best approach in order to have a UISegmentedControl appears
I am wondering what the best approach is for creating a horizontal list with
I'm wondering what the best approach is to verify that a class has all
Was just wondering what the best approach is when creating a multi-lingual website that
I'm wondering about the best approach to designing a DB connection manager for a
I am wondering what is the best approach/methods/technology to implement a C# desktop application
I am wondering if someone can help me figure out the best approach to
I was wondering, what's the best approach in creating users authentication for my asp.net-mvc
I'm wondering the best way to start a pthread that is a member of
I was wondering what the best approach would be to search a database via

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.