【发布时间】:2013-04-10 11:51:28
【问题描述】:
我目前正在使用 Linq to Entities 执行搜索。
如果我想通过客户的参考代码搜索,我会使用这种方法:GetClientByReference("A01")
public IQueryable<DataLayer.Client> GetClientByReference(String reference)
{
return new DataLayer.DlClient(this.Entities).GetClientByReference(reference);
}
如果我想按客户姓名搜索,我会使用这种方法:GetClientByName("Joe")
public IQueryable<DataLayer.Client> GetClientByName(String name)
{
return new DataLayer.DlClient(this.Entities).GetClientByName(name);
}
显然,就可扩展性而言,这非常糟糕,因为我需要将方法组合在一起,并为每个可搜索字段编写一个 (GetClientByDateAndReferenceAndName??) 有什么方法可以使这个更通用吗?
理想情况下,我希望能够执行以下操作:
GetClient("Reference", "A01")GetClient("Name", "Joe")GetClient("Reference", "A01", "Name", "Joe")
【问题讨论】:
-
你是怎么解决的?您是否使用了
Expression<Func<Client, bool>>或者建议的Func<Client, bool>是否也可以在内部返回所有客户端而不是请求的客户端? -
我使用“原样”的答案,使用
Func<Client, bool> filter
标签: c# asp.net .net linq entity-framework