Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3911

LINQ3: OrderBy/OrderByDescending with expression throws ExpressionNotSupportedException

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: LINQ3

       

      var utcNow = DateTime.UtcNow; 
      ... 
      var query = GetCollection<TEntity>().AsQueryable().Where(n => !n.StartDateUtc.HasValue || n.StartDateUtc <= utcNow);
      query = query.Where(n => !n.EndDateUtc.HasValue || n.EndDateUtc >= utcNow); 
      query.OrderByDescending(n => n.StartDateUtc ?? n.CreatedOnUtc);
      ...
      query.Count();

       

      Exception has occurred: CLR/MongoDB.Driver.Linq.Linq3Implementation.ExpressionNotSupportedExceptionAn exception of type 'MongoDB.Driver.Linq.Linq3Implementation.ExpressionNotSupportedException' occurred in MongoDB.Driver.dll but was not handled in user code: 'Expression not supported: n => (n.StartDateUtc ?? n.CreatedOnUtc).'   at MongoDB.Driver.Linq.Linq3Implementation.Misc.LambdaExpressionExtensions.GetFieldPath(LambdaExpression fieldSelectorLambda, TranslationContext context, IBsonSerializer parameterSerializer)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.OrderByMethodToPipelineTranslator.CreateSortField(TranslationContext context, String methodName, LambdaExpression keySelector, IBsonSerializer parameterSerializer)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.OrderByMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.CountMethodToExecutableQueryTranslator.Translate[TDocument](MongoQueryProvider`1 provider, TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.TranslateScalar[TDocument,TResult](MongoQueryProvider`1 provider, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](Expression expression)
         at System.Linq.Queryable.Count[TSource](IQueryable`1 source)

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            alexey.a@nopcommerce.com Alexey Anohin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: