如何为多个表创建通用方法?我的桌子:
- 顾客
- 员工
我需要使用通用方法搜索它们。我尝试了以下方法:
称呼:
var predicateSearchCustomer = GetSearchPredicate<Customer>(search,Types.Customer);
var predicateSearchEmployee = GetSearchPredicate<Employee>(search,Types.Employee);
我的方法:
public Expression<Func<T, bool>> GetSearchPredicate<T>(string parametrs, Types Types)
{
var predicateInner = PredicateBuilder.False<T>();
var paramertsForSearch = new List<string>(parametrs.Split(' '));
paramertsForSearch.RemoveAll(string.IsNullOrEmpty);
foreach (var item in paramertsForSearch)
{
var itemSearch = item;
switch (Types)
{
case Types.Customer:
predicateInner = predicateInner.Or(x => x.CustomerName.Contains(itemSearch));
predicateInner = predicateInner.Or(x => x.CustomerFamily.Contains(itemSearch));
break;
case Types.Employee:
predicateInner = predicateInner.Or(x => x.EmployeeName.Contains(itemSearch));
predicateInner = predicateInner.Or(x => x.EmployeeFamily.Contains(itemSearch));
break;
}
}
return predicateInner;
}
错误:
错误 5“T”不包含“CustomerName”的定义,并且找不到接受“T”类型的第一个参数的扩展方法“CustomerName”(您是否缺少 using 指令或程序集引用?)
如何转换T
为Customer
or Employee
?