Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

MongoDB.Driver Resolve Expression Throw Exception

The MongoDB.Driver version :

When I use Expression<Func<TDocument, bool>> filter as following

Throw Exception :

System.ArgumentException: Unsupported filter: Invoke(x => ((Convert(x.FromDate, Nullable`1) >= 2020/6/8 7:12:07) AndAlso (Convert(x.FromDate, Nullable`1) <= 2020/6/8 8:12:07)), {document}).
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate[TDocument](Expression`1 predicate, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.ExpressionFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.MongoCollectionImpl`1.CreateCountDocumentsOperation(FilterDefinition`1 filter, CountOptions options)
   at MongoDB.Driver.MongoCollectionImpl`1.CountDocumentsAsync(IClientSessionHandle session, FilterDefinition`1 filter, CountOptions options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass33_0.<CountDocumentsAsync>b__0(IClientSessionHandle session)   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)

The Expression.True Extesions scource:

/// <summary>
/// True
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> True<T>() => parameter => true;

/// <summary>
/// False
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> False<T>() => parameter => false;

/// <summary>
/// Or
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="this"></param>
/// <param name="other"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> @this, Expression<Func<T, bool>> other)
{
    var invokedExpr = Expression.Invoke(other, @this.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.OrElse(@this.Body, invokedExpr), @this.Parameters);
}

/// <summary>
/// And
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="this"></param>
/// <param name="other"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> @this, Expression<Func<T, bool>> other)
{
    var invokedExpr = Expression.Invoke(other, @this.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(@this.Body, invokedExpr), @this.Parameters);
}