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

发布于 2024-12-04 23:21:12 字数 1483 浏览 0 评论 0原文

想知道动态过滤内存列表的最佳方法是什么。

假设我有内存列表,我需要编写一个查找,根据条件过滤内存列表。 显然,如果一个值为 null 或空,请使用它。 如何构建谓词。我看过 predicateBuilder 但不知道如何使用它。

请参阅下面的代码

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

有什么建议吗? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2024-12-11 23:21:12

当然,您只需使用 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;
}

编辑:如果您愿意,您可以在 DateTime? 中使用提升的运算符:

 if (criteria.StartDate.HasValue && criteria.EndDate.HasValue)
 {
     results = results.Where(c => c.DateOfBirth != null &&
                                  c.DateOfBirth >= criteria.StartDate &&
                                  c.DateOfBirth < criteria.EndDate);
 }

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);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文