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

IMongoCollection.AsQueryable().Select() fails for array type (regression)

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.19.0, 2.23.1
    • Component/s: LINQ3
    • Labels:
      None
    • Dotnet Drivers

      Summary

       
      // IMongoCollection<TestClass> collection;collection.AsQueryable().Select(t => (IEnumerable<int>) new[] { t.Integer }).ToArray()
      throws the following exception on 2.23.1:

      MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: t => new [] {t.Integer} because lambda body type is not convertible to lambda return type.
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.SelectMethodToPipelineTranslator.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.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute()
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator()
         at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
         at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
         at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      2.23.1

      Additional Background

      This works on 2.18.0. From 2.19.0 it fails, though with a different (worse) exception message:{{{}
      {}}}

      System.InvalidCastException: Unable to cast object of type 'MongoDB.Driver.Linq.Linq3Implementation.Serializers.WrappedValueSerializer`1[System.Collections.Generic.IEnumerable`1[System.Int32]]' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Int32[]]'.
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExecutableQuery`3.CreateCollectionPipelineDefinition()
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExecutableQuery`3.Execute(IClientSessionHandle session, CancellationToken cancellationToken)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute()
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator()
         at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
         at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
         at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

       

      A workaround is to cast the lambda result to IEnumerable<T>:

      .AsQueryable().Select(t => (IEnumerable<int>) new[] { t.Integer }).ToArray();
       
      This is a bit of a "gotcha" though, as it's hard to find all places in our code that may be affected by it (and of course the results then have to be cast back to array somewhere if the caller expects that).

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            evgeny+mongodb@loop54.com Evgeny Morozov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: