【发布时间】:2020-09-28 07:46:10
【问题描述】:
我有一个接受Func<T,bool> 条件作为参数的方法,我想将条件扩展为:
public T SomeClass.GETConditionalValue(x=>x.Id==id).ToList();
现在这个SomeClass.GETConditionalValue 需要一个参数Func<T,bool> 条件
我想修改它而不打扰SomeClass.GETConditionalValue
类似这样的:
public T SomeClass.GETConditionalValue(ApplyFilter(x=>x.Id==id)).ToList();
在哪里
public Func<T,bool>ApplyFilter<T>(Func<T,bool>condition) where T: GenericCLass
{
return condition (+ some new condition in & or || block)
}
**UPDATE :- ** 不能在这里使用委托,抛出无法将 lemda 表达式转换为 bool 类型,因为它不是委托类型
public IEnumerable<T> GetEntities<T>(Func<T, bool> condition) where T : ITableEntity, new()
{
if (applicationFilter == null)
return tableContext.CreateQuery<T>().Where(condition).ToList();
return tableContext.CreateQuery<T>().Where(applicationFilter).Where(condition).ToList();
}
我的电话是
var mappedApplications = userToMdouleRepo.GetEntities<UserMappingToModules>(x => x.UserEmail == email)
这里我想改一下
var mappedApplications = userToMdouleRepo.GetEntities<UserMappingToModules>(ApplyAdditionalFilter(x => x.UserEmail == email)).ToList()
【问题讨论】:
-
换句话说,
ApplyFilter应该返回一个委托? -
是的,问题是您可以像 (condition && x=>x.Name=="Hi") 一样扩展 func 条件
-
@Fildor 我不能使用代理
-
您正在使用委托:Func<T,TResult> Delegate
-
我的意思是我继续扩展它的抛出错误的条件