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

Comparisons in a LINQ Select projection should use the field serializer to serialize constants

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.14.0
    • Affects Version/s: 2.1
    • Component/s: Linq, LINQ3

      When projecting a boolean value derived from comparing a field to a constant the constant should be serialized using the field's serializer.

      One example showing why this is necessary is:

      public enum E { A, B }
      
      public class C
      {
          public int Id { get; set; }
          [BsonRepresentation(BsonType.String)]
          public E E { get; set; }
      }
      
      public static class Program
      {
          public static void Main(string[] args)
          {
              var client = new MongoClient("mongodb://localhost");
              var database = client.GetDatabase("test");
              var collection = database.GetCollection<C>("test");
      
              var queryable = collection.AsQueryable().Select(x => x.E == E.A);
              Console.WriteLine(queryable.ToString());
          }
      }
      

      The output is:

      aggregate([{ "$project" : { "__fld0" : { "$eq" : ["$E", 0] }, "_id" : 0 } }])
      

      but should be:

      aggregate([{ "$project" : { "__fld0" : { "$eq" : ["$E", "A"] }, "_id" : 0 } }])
      

      (i.e. "$E" should be compared to "A", not 0)

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: