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

Using some LINQ function after a Union throws an Exception

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Dotnet Drivers
    • 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?

      Summary

      We used LINQ to "Union" two collections and "Join" in a third one. When we tried that the query throws an error:

         System.InvalidOperationException : More than one possible serializer found for <>f__AnonymousType0`2[System.String,System.Boolean] in x.

      This is true for versions 2.27.0, 2.26.0, 2.25.0 of the MongoDB.Driver nuget package.

      How to Reproduce

      write followin statement and try to execute it.

                  var queryable = _firstCollection
                      .AsQueryable()
                      .Where(c => c.Name.StartsWith("second"))
                      .Union(_secondCollection.AsQueryable()
                          .Where(c => c.Name.StartsWith("another")))
                      .Where(x => x.Id == 2);

      First of all, it's not possible to chain an Where after the Union because it does not return an "IMongoQuerable<T>". This is easy solved with:

       

              public static IMongoQueryable<TSource> Union<TSource>(this IMongoQueryable<TSource> source1, IEnumerable<TSource> source2)
              {
                  Ensure.IsNotNull(source1, nameof(source1));
                  Ensure.IsNotNull(source2, nameof(source2));
                  return (IMongoQueryable<TSource>)Queryable.Union(source1, source2);
              }
      

      But after that it throws the Exception from above.

      I figured out an workaround how to fix it, but I am not able to run the whole set of unit tests that are available in the project, because it seems like it needs an local running mongodb to perform them.

      So I don't know if this is a proper fix or there maybe an better solution for this issue.

      I will create an PR with what I have and hopefully you can give some feedback on that. 

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            616b2f@gmail.com Alexej Kowalew
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: