c# - 将 Predicate<T> 转换为 Expression<Func<T, bool>>

标签 c# expression iqueryable predicate icollectionview

可以转换 Predicate<T> to Expression<Func<T, bool>>以某种方式?

我想使用我的 ICollectionView 的过滤器来使用下一个 IQueryable 函数:

public static System.Linq.IQueryable<TSource> Where<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<System.Func<TSource, bool>> predicate)

谢谢

最佳答案

是这样的吗?

Predicate<string> predicate = input => input.Length > 0;
Expression<Func<string, bool>> expression = (input) => predicate(input);

您可以为您的 ICollectionView 创建一个扩展 Where 方法,它接受一个谓词,将其转换为这样的表达式,然后调用 Linq 提供的 Where 方法。

public static IQueryable<T> Where(this IQueryable<T> source, Predicate<T> predicate)
{
    return source.Where(x => predicate(x));
}

关于c# - 将 Predicate<T> 转换为 Expression<Func<T, bool>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802269/

相关文章:

php - 使用 PHP 删除空格

linux - 查找在文件中搜索特定模式的正则表达式 (Unix-Solaris)

java - 可重复使用的条件/表达式类

c# - 如何使用表达式树过滤包含可为空类型的 IQueryable?

c# - 如何覆盖 MongoDB Gridfs 中的文件?

c# - 具有多个 where 子句的 Entity Framework Core LINQ 查询

c# - 日期时间.Parse : exception when changing culture

c# - 操作方法的细粒度访问控制

c# - .NET 核心中的 IQueryable OrderBy 表达式

c# - 为 IQueryable<T> 创建自定义 OrderByDescending()