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

One-to-many LINQ query using Join fails with InvalidOperationException

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.14.1
    • Component/s: LINQ3
    • Labels:
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      If you try to reformulate the query in CSHARP-4053 using a join to the person.MovieIds enumerable, an InvalidOperationException is thrown:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using MongoDB.Bson;
      using MongoDB.Driver;
      using MongoDB.Driver.Linq;
      
      var settings = new MongoClientSettings { LinqProvider = LinqProvider.V3 };
      var client = new MongoClient(settings);
      var db = client.GetDatabase("test");
      var people = db.GetCollection<Person>("people");
      var movies = db.GetCollection<Movie>("movies");
      
      var query = from person in people.AsQueryable()
                  from movieId in person.MovieIds
                  join movie in movies.AsQueryable() on movieId equals movie.Id
                  select new { person, movie };
      
      foreach (var tuple in query.ToList())
      {
          Console.WriteLine(tuple);
      }
      
      class Movie
      {
          public ObjectId Id { get; set; }
      }
      
      class Person
      {
          public IEnumerable<ObjectId> MovieIds { get; set; }
      }
      

      The exception is:

      /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/tests/MongoDB.Driver.TestConsoleApplication/bin/Debug/netcoreapp3.1/MongoDB.Driver.TestConsoleApplication 
      Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object.
         at MongoDB.Driver.Linq.Linq3Implementation.Serializers.WrappedValueSerializer`1.TryGetMemberSerializationInfo(String memberName, BsonSerializationInfo& serializationInfo) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/WrappedValueSerializer.cs:line 77
         at MongoDB.Driver.Linq.Linq3Implementation.Misc.DocumentSerializerHelper.HasFieldInfo(IBsonSerializer serializer, String memberName) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Misc/DocumentSerializerHelper.cs:line 40
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MemberExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, MemberExpression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MemberExpressionToAggregationExpressionTranslator.cs:line 46
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ExpressionToAggregationExpressionTranslator.cs:line 67
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ExpressionToAggregationExpressionTranslator.cs:line 115
         at MongoDB.Driver.Linq.Linq3Implementation.Misc.LambdaExpressionExtensions.GetFieldPath(LambdaExpression fieldSelectorLambda, TranslationContext context, IBsonSerializer parameterSerializer) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Misc/LambdaExpressionExtensions.cs:line 27
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.JoinMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/JoinMethodToPipelineTranslator.cs:line 52
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/ExpressionToPipelineTranslator.cs:line 44
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToExecutableQueryTranslators/ExpressionToExecutableQueryTranslator.cs:line 34
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute() in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/MongoQuery.cs:line 66
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator() in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/src/MongoDB.Driver/Linq/Linq3Implementation/MongoQuery.cs:line 78
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
         at Program.<Main>$(String[] args) in /Users/james/Dropbox/code/mongodb/mongo-csharp-driver/tests/MongoDB.Driver.TestConsoleApplication/Program.cs:line 19
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            james.kovacs@mongodb.com James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: