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

Any subquery fails for Nullable<ObjectId> comparison

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.8.3
    • Component/s: None
    • Labels:
      None
    • Fully Compatible

      Take these queries and try to run them. You will get the following exception:

      ObjectId?[] supplierIds =
      (from s in mongo.Suppliers
      where s.Country == supplierCountry
      select new Nullable<ObjectId>(s.Id)).ToArray();

      var supplierProducts =
      (from p in mongo.Products
      where supplierIds.Any(id => id == p.SupplierID)
      orderby p.ProductName
      select p);

      Unhandled Exception: System.NotSupportedException: Unable to determine the serialization information for the expression: Nullable<ObjectId>[]:

      { 000000000000000000000016, 000000000000000000000019, 000000000000000000000002, 000000000000000000000003 }

      .
      at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo
      (Expression node, Dictionary`2 serializationInfoCache)
      at MongoDB.Driver.Linq.PredicateTranslator.BuildAnyQuery(MethodCallExpression
      methodCallExpression)
      at MongoDB.Driver.Linq.PredicateTranslator.BuildMethodCallQuery(MethodCallExp
      ression methodCallExpression)
      at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
      at MongoDB.Driver.Linq.SelectQuery.BuildQuery()
      at MongoDB.Driver.Linq.SelectQuery.Execute()
      at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression)
      at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator()

      An alternate attempt at that query with ObjectId[] rather than ObjectId[] gives basically the same failure:

      ObjectId[] supplierIds =
      (from s in mongo.Suppliers
      where s.Country == supplierCountry
      select s.Id).ToArray();

      var supplierProducts =
      (from p in mongo.Products
      where supplierIds.Any(id => id == p.SupplierID)
      orderby p.ProductName
      select p);

      However, if I use Contains rather than Any, it works! But it is more painful because I need to convert to an nullable ObjectId array when the source is not actually nullable (required so supplierIds.Contains() compiles):

      ObjectId?[] supplierIds =
      (from s in mongo.Suppliers
      where s.Country == supplierCountry
      select new Nullable<ObjectId>(s.Id)).ToArray();

      var supplierProducts =
      (from p in mongo.Products
      where supplierIds.Contains(p.SupplierID)
      orderby p.ProductName
      select p);

      I've attached the two related classes involved in the query.

        1. Product.cs
          0.7 kB
        2. Supplier.cs
          0.7 kB

            Assignee:
            Unassigned Unassigned
            Reporter:
            mkennedy66996693 Michael Kennedy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: