C# 如何使用linq动态过滤内存中的列表?
想知道动态过滤内存中列表的最佳方法是什么 假设我有一个内存列表,我需要编写一个查找,根据条件过滤内存列表。 显然,如果值为null或空,请使用它。 如何构建谓词。我看过predicateBuilder,但不知道如何使用它 请参阅下面的代码C# 如何使用linq动态过滤内存中的列表?,c#,linq,linq-to-objects,C#,Linq,Linq To Objects,想知道动态过滤内存中列表的最佳方法是什么 假设我有一个内存列表,我需要编写一个查找,根据条件过滤内存列表。 显然,如果值为null或空,请使用它。 如何构建谓词。我看过predicateBuilder,但不知道如何使用它 请参阅下面的代码 public class CustomerService { private IEnumerable<Customer> customersInMemoryAlready; public CustomerService()
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; }
}
公共类客户服务
{
private IEnumerable CustomersInMemoryReady;
公共客户服务()
{
customersInMemoryAlready=new List();//假设我们在这里接待了200名客户
}
公共IEnumerable查找(标准)
{
IEnumerable results=GetInMemoryCustomers();
//现在,根据标准进行筛选。您将如何进行筛选
如果(!string.IsNullOrEmpty(criteria.姓氏))
{
//?
}
如果(!criteria.StartDate.HasValue&!criteria.EndDate.HasValue)
{
//?获取开始日期和结束日期之间的所有出生日期
}
返回结果;
}
私有IEnumerable GetInMemoryCustomers()
{
返回CustomerSineMoryalReady;
}
}
公共类客户
{
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
公共日期时间?出生日期{get;set;}
公共字符串City{get;set;}
}
公共类标准
{
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
公共日期时间?开始日期{get;set;}
公共日期时间?结束日期{get;set;}
公共字符串City{get;set;}
}
有什么建议吗?
谢谢当然,您只需使用
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;
}
为什么在检查后使用Value属性!HasValue,这也是一个问题,但我不明白你的意思?@anivas:虽然你可以使用解除的操作符,但我个人觉得不使用更清楚。至少,我今天下午是这样做的:)我会加上一条注释。我的问题比这更简单(更愚蠢),我想知道为什么startDate和EndDate在为空时使用?@Jon Skeet。谢谢你的回复。我在谷歌上找到了你的答案,我想知道“委托谓词生成器”是否有用,以及你是如何使用它的it@anivas:啊!好问题-我怀疑这是一个错误,然后我去复制:)现在已经修复。
if (criteria.StartDate.HasValue && criteria.EndDate.HasValue)
{
results = results.Where(c => c.DateOfBirth != null &&
c.DateOfBirth >= criteria.StartDate &&
c.DateOfBirth < criteria.EndDate);
}