【发布时间】:2019-03-03 23:39:02
【问题描述】:
假设我有一个Expression<Func<Arg1,Arg2,ReturnType>>,如何将其转换为Expression<Func<Arg1,ReturnType>>?
我已经定义了一个Expression<Func<Individual, Record, bool>>,如果允许个人查看记录,它将返回true。我想在我的 DbContext 的 Where 子句中为个人或记录使用这个表达式。示例:
Expression<Func<Individual,Record,bool>> expression = GetFilterExpression();
// Get All the records that an individual can review...
Individual ind = SomeIndividual();
Expression<Func<Individual,bool>> canBeViewedBy = Utility.ReplaceParameter(expression, 0, ind);
List<Record> records = _context.Records.Where(canBeViewedBy).ToList();
// Get All the individuals that can review the record...
Record record = SomeRecord();
Expression<Func<Record,bool>> canView = Utility.ReplaceParameter(expression, 1, record);
List<Individual> individuals = _context.Individuals.Where(canView).ToList();
在我的实际应用程序中,我有一个CanIndividualViewRecordSpecification,它当前生成一个Expression<Func<Record,bool>>。我希望它也能够生成Expression<Func<Individual,bool>>,而不必多次定义过滤器。
【问题讨论】:
标签: c# entity-framework linq functional-programming