首页 > 解决方案 > 始终返回 true 的替代表达式

问题描述

我正在尝试构建一个动态Expression<Func<User, bool>>以在 MongoDB LINQ Where 查询过滤器中使用。

我正在使用 if-else 块动态构建表达式,并使用 Albahari PredicateBuilder.And()( http://www.albahari.com/nutshell/predicatebuilder.aspx ) 组合表达式。

PredicateBuilder.True<T>()在这些块中使用AND会导致 MongoDB LINQ 转换器抛出此异常:

Unsupported filter: Invoke(f => True, {document}).

我可以用什么其他表达式格式来表达user=>true某种逻辑,以便 MongoDB LINQ 提供程序接受它?

注意:我尝试使用 return Expression.Lambda(Expression.Constant(true), Expression.Parameter(type, "_")); 我在另一篇 SO 帖子(https://stackoverflow.com/a/36773532/10811808)中看到,但我不知道如何将此 ConstantExpression 转换为 Expression>。

标签: c#mongodblinq

解决方案


推荐阅读