c# - 在 IQueryable 上应用内部表达式过滤器
问题描述
假设我有以下对象:
public class Source { public string Name; }
public class Target { public string Name; }
public class Result
{
public Source SourceObj;
public Target TargetObj;
}
现在,IQueryable<Result>
从某个地方获取我想为它准备表达式过滤器,只需将目标过滤器作为表达式:Expression<Func<Target, bool>> filter
。过滤器方法签名如下所示:
public Expression<Func<Result, bool>> Filter(IQueryable<Result> collection, Expression<Func<Target, bool>> targetFilter)
{
in result expression: "in given collection select items where their property TargetObj satisfies targetFilter"
}
任何建议将不胜感激。谢谢
解决方案
我不确定我是否正确理解了您的目标,但我认为这可能是您想要的。
使用 linq Select
,您可以映射源集合中的元素。所以在这里我们将结果映射到目标,然后应用您的目标过滤器。
public IQueryable<Target> GetFilteredTargets(IQueryable<Result> collection, Expression<Func<Target, bool>> targetFilter)
{
return collection.Select(result => result.Target).Where(targetFilter);
}
推荐阅读
- c# - 从互联网问题c#中进度和下载大文件
- reactjs - React Webpack 不渲染 DOM(未定义 regeneratorRuntime)
- tensorflow - TFLite模型转换器不输出uint8
- java - 为递归或迭代调用计算中间索引的最佳方法是什么
- postgresql - JOIN 是一种什么样的连接?
- arrays - 如何以简写形式将值分配给模式
- api - AutoDesk forge 有邮递员收藏吗?
- c# - 正则表达式模式,如果后跟一个点,可以重复任意次数
- reactjs - 在 freeSolo 模式下使用 Material UI 自动完成和 react-hook-form
- curl - 如何在烧瓶中调用带有参数的外部 API url?