LINQ query for appended string with Object ID throws an exception

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Won't Do
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: LINQ
    • 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?
    • None
    • None
    • None
    • None
    • None
    • None

      In versions 2.26.0 to 3.1.0, the .NET/C# drivers throws a System.FormatException 

      using MongoDB.Driver;
      using MongoDB.Driver.Linq;
      using MongoDB.Bson;
      using MongoDB.Bson.Serialization.Attributes;
      
      namespace TestApp {
      
        class Program {
      
          static async Task Main(string[] args) {
      
            string connectionString = "<connection_string>";
            string databaseName = "entity";
            string collectionName = "entity-folders";
            var client = new MongoClient(connectionString);
            Console.WriteLine("connected");
            var db = client.GetDatabase(databaseName);
            var collection = db.GetCollection < FolderEntity > (collectionName);
      
            try {
              var query = collection.AsQueryable()
                .Where(x => x.Name.Equals("prefix" + x.Id.ToString()));        var res = await query.ToListAsync();
              Console.WriteLine($"Success: {res.Count}");
            } 
            catch (Exception ex) {
              Console.WriteLine($"Error: {ex.Message}");
            }
          }
        }  
      
      public record FolderEntity {
          [BsonId, BsonRepresentation(BsonType.ObjectId)]
          public required string Id {get; set;}
          public required string Name {get; set;}
        }
      }

      "Error: 'prefix' is not a valid 24 digit hex string."

      The same is not the case for v3.2.0+ or even v2.25.0. The query succeeds with those versions. The behavior is the same irrespective of whether a POCO is used or a record. 

            Assignee:
            Robert Stam
            Reporter:
            Rishit Bhatia
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: