1

I am trying to build a dynamic Expression<Func<User, bool>> to use in MongoDB LINQ Where query filter.

I am building the expression dynamically using if-else blocks and combining the expressions using Albahari's PredicateBuilder.And() (http://www.albahari.com/nutshell/predicatebuilder.aspx).

Using PredicateBuilder.True<T>() among these AND blocks causes the MongoDB LINQ translator to throw this exception:

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

In what other expression format can I express user=>true kind of logic so that MongoDB LINQ provider will accept it?

Note: I tried using return Expression.Lambda(Expression.Constant(true), Expression.Parameter(type, "_")); which I saw in another SO post (https://stackoverflow.com/a/36773532/10811808) but I don't know how to convert this ConstantExpression to Expression>.

YoungMonk
  • 63
  • 6
hakanviv
  • 223
  • 2
  • 12
  • 1
    May be the problem is not with constant expression, but invocation (`Expresseion.Invoke`) used by the original `PredicateBuilder` implementation. You could try different `PredicateBuilder` implementations - for instance [Universal PredicateBuilder](https://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/), https://stackoverflow.com/questions/36246162/establish-a-link-between-two-lists-in-linq-to-entities-where-clause/36247259#36247259 or similar – Ivan Stoev Jan 11 '19 at 10:00
  • 1
    @IvanStoev You are exactly right, thanks. Using the Univeral PredicateBuilder solved the problem. – hakanviv Jan 11 '19 at 10:39

0 Answers0