如何将我的谓词转换为 C# 中的通用谓词?
How to convert my predicate to a generic predicate in C#?
我有一个完美运行的谓词代码,但我想减少我的代码并将其转换为通用代码并在我的存储库中使用它
public abstract class RequestParameters
{
const int maxPageSize = 5;
public int PageNumber { get; set; } = 1;
private int _pageSize = 10;
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = (value > maxPageSize) ? maxPageSize : value;
}
}
public string OrderBy { get; set; }
public string Fields { get; set; }
}
这是我的 EmployeeSearchModel
public class EmployeeSearchModel : RequestParameters
{
public EmployeeSearchModel()
{
OrderBy = "name";
}
public string Name { get; set; }
public int Age { get; set; }
public string Position { get; set; }
public Expression<System.Func<Employee, bool>> Go()
{
var predicate = PredicateBuilder.True<Employee>();
if (!string.IsNullOrEmpty(Name))
{
predicate = predicate.And(i => i.Name.ToLower().Contains(Name.ToLower()));
}
if (!string.IsNullOrEmpty(Position))
{
predicate = predicate.And(i => i.Position.ToLower().Contains(Position.ToLower()));
}
return predicate;
}
}
这是我的存储库
public async Task<PagedList<Employee>> GetEmployeesAsyncExp(EmployeeSearchModel employeeParameters)
{
Expression f = employeeParameters.Go();
var employees = await FindByCondition(employeeParameters.Go())
.OrderBy(e => e.Name)
.ToListAsync()....
.....
现在我想将其更改为类似这样的通用内容:
public class EmployeeSearchModel<T> : RequestParameters ...
好吧,这里没有真正的问题,还有很多缺失的信息和错误的信息。我想您会希望交换“Employee”class 以使用您的 SearchModel 部分过滤其他 classes。因此“EmployeeSearchModel”是错误的,因为它仍然包含 Employee。
另一方面,您的方法使用了一些特定于员工的部分,例如姓名或年龄,除非您的其他 class 也包含年龄和姓名,否则不能将其用作通用方法。
要重用 SearchModel,最好的办法是将其更改为抽象 class 并让 EmployeeSearchModel 继承它:
public abstract class SearchModel<T> : RequestParameters
{
public abstract Expression<System.Func<T, bool>> Go();
}
public class EmployeeSearchModel : SearchModel<Employee>
{
public EmployeeSearchModel()
{
OrderBy = "name";
}
public string Name { get; set; }
public int Age { get; set; }
public string Position { get; set; }
public override Expression<Func<Employee, bool>> Go()
{
var predicate = PredicateBuilder.True<Employee>();
if (!string.IsNullOrEmpty(Name))
{
predicate = predicate.And(i => i.Name.ToLower().Contains(Name.ToLower()));
}
if (!string.IsNullOrEmpty(Position))
{
predicate = predicate.And(i => i.Position.ToLower().Contains(Position.ToLower()));
}
return predicate;
}
}
然后您还可以使用它来搜索其他 classes,例如公司:
public class CompanySearchModel : SearchModel<Company>
{
public string Department {get; set;}
public override Expression<Func<Company, bool>> Go()
{
var predicate = PredicateBuilder.True<Company>();
if (!string.IsNullOrEmpty(Department))
{
predicate = predicate.And(x => x.Department.ToLower().Contains(Department.ToLower()));
}
}
}
我有一个完美运行的谓词代码,但我想减少我的代码并将其转换为通用代码并在我的存储库中使用它
public abstract class RequestParameters
{
const int maxPageSize = 5;
public int PageNumber { get; set; } = 1;
private int _pageSize = 10;
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = (value > maxPageSize) ? maxPageSize : value;
}
}
public string OrderBy { get; set; }
public string Fields { get; set; }
}
这是我的 EmployeeSearchModel
public class EmployeeSearchModel : RequestParameters
{
public EmployeeSearchModel()
{
OrderBy = "name";
}
public string Name { get; set; }
public int Age { get; set; }
public string Position { get; set; }
public Expression<System.Func<Employee, bool>> Go()
{
var predicate = PredicateBuilder.True<Employee>();
if (!string.IsNullOrEmpty(Name))
{
predicate = predicate.And(i => i.Name.ToLower().Contains(Name.ToLower()));
}
if (!string.IsNullOrEmpty(Position))
{
predicate = predicate.And(i => i.Position.ToLower().Contains(Position.ToLower()));
}
return predicate;
}
}
这是我的存储库
public async Task<PagedList<Employee>> GetEmployeesAsyncExp(EmployeeSearchModel employeeParameters)
{
Expression f = employeeParameters.Go();
var employees = await FindByCondition(employeeParameters.Go())
.OrderBy(e => e.Name)
.ToListAsync()....
.....
现在我想将其更改为类似这样的通用内容:
public class EmployeeSearchModel<T> : RequestParameters ...
好吧,这里没有真正的问题,还有很多缺失的信息和错误的信息。我想您会希望交换“Employee”class 以使用您的 SearchModel 部分过滤其他 classes。因此“EmployeeSearchModel”是错误的,因为它仍然包含 Employee。
另一方面,您的方法使用了一些特定于员工的部分,例如姓名或年龄,除非您的其他 class 也包含年龄和姓名,否则不能将其用作通用方法。
要重用 SearchModel,最好的办法是将其更改为抽象 class 并让 EmployeeSearchModel 继承它:
public abstract class SearchModel<T> : RequestParameters
{
public abstract Expression<System.Func<T, bool>> Go();
}
public class EmployeeSearchModel : SearchModel<Employee>
{
public EmployeeSearchModel()
{
OrderBy = "name";
}
public string Name { get; set; }
public int Age { get; set; }
public string Position { get; set; }
public override Expression<Func<Employee, bool>> Go()
{
var predicate = PredicateBuilder.True<Employee>();
if (!string.IsNullOrEmpty(Name))
{
predicate = predicate.And(i => i.Name.ToLower().Contains(Name.ToLower()));
}
if (!string.IsNullOrEmpty(Position))
{
predicate = predicate.And(i => i.Position.ToLower().Contains(Position.ToLower()));
}
return predicate;
}
}
然后您还可以使用它来搜索其他 classes,例如公司:
public class CompanySearchModel : SearchModel<Company>
{
public string Department {get; set;}
public override Expression<Func<Company, bool>> Go()
{
var predicate = PredicateBuilder.True<Company>();
if (!string.IsNullOrEmpty(Department))
{
predicate = predicate.And(x => x.Department.ToLower().Contains(Department.ToLower()));
}
}
}