【发布时间】:2015-08-29 09:39:05
【问题描述】:
如何将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
}
和
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
}
);
【问题讨论】:
-
你能举一个具体的例子来说明你想要达到的目标吗?
-
我想将“DTO where条件”从业务层传递到访问层
public DepartmentViewModel GetSingle(System.Linq.Expressions.Expression<Func<DepartmentViewModel, bool>> whereCondition)到public IEnumerable< Department> GetAll(Func< Department, bool> predicate = null) -
如何从
Department转换为DepartmentViewModel?
标签: c# lambda expression