简体   繁体   English

如何使用linq动态过滤内存中的列表?

[英]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. 显然,如果值是null或为空,请使用它。 How do you build the predicate. 您如何建立谓词。 I have looked at the predicateBuilder but not sure how to use it. 我看过predicateBuilder,但不确定如何使用它。

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 : 当然,您只需使用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? 编辑:您可以为DateTime?使用解除运算符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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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