可以转换 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/