我该如何将Func<DepartmentViewModel, bool>
转换为Func<Department, bool>
?
我已经查看了很多关于此问题的帖子,但都没有帮助到我。
我调用的是以下函数:
public DepartmentViewModel GetSingle(Expression<Func<DepartmentViewModel, bool>> whereCondition)
从 GUI 层面来看,代码如下:
_departmentService.GetSingle(de => de.Id ==id));
在我的业务层中,GetSingle
函数内部,我必须调用
public IEnumerable<Department> GetAll(Func<Department, bool> predicate = null)
但是GetAll
函数接受一个Func<Department, bool>
类型参数
这是我的对象:
class Department {
public string name
}
and
class DepartmentViewModel{
public string name
}
关于这个问题,我找到了最佳答案:
Func<DepartmentViewModel, bool> some_function = whereCondition.Compile();
Func<Department, bool> converted = d => some_function(
new DepartmentViewModel {
Id=d.Id,
Description=d.Descriptions
}
);
以上代码是将一个DepartmentViewModel
类型的对象转换为Department
类型的对象。其中whereCondition
是一个用于筛选数据的条件。
public DepartmentViewModel GetSingle(System.Linq.Expressions.Expression<Func<DepartmentViewModel, bool>> whereCondition)
转换为public IEnumerable<Department> GetAll(Func<Department, bool> predicate = null)
。 - Faraz jaliliDepartment
转换为DepartmentViewModel
? - Douglas