简体   繁体   中英

How do you filter an in memory list dynamically using linq?

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

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);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM