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

enum equality query on Queryable

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: Minor - P4 Minor - P4
    • None
    • 1.5
    • None
    • Win 7, 64bit
    • Fully Compatible

    Description

      Given the following poco and contained enum

      User.cs

      class User
      {
        public UserType Type {get;set;}
      }

      UserType.cs

      enum UserType
      {
        Admin,
        General,
        Affiliate
      }

      The following code does not return the contained Affiliate users.

      ///assuming multiple affiliate users are already in the collection
      IQueryable<User> result;
      using (var db = ...)
      {
        var queryable = db.GetCollection<User>("users").AsQueryable();
        result = (from user in queryable
                    where user.Type == UserType.Affiliate
                    select user);
      }
      result.Count(); // zero

      But retrieving all users than filtering on the Type works as expected

      var list = db.GetCollection<User>("users").AsQueryable().ToList();
        list.Count(u => u.Type == UserType.Affiliate); // gt zero

      It's a bit odd that the Type gets deserialized appropriately but the query tree is built using a value of the enum that does not equate the value(string) stored in the db by default. I'm not certain what value the query tree is using but registering the class map as follows resolved my issue(note:this is how the db is already storing the enum):

      BsonClassMap.RegisterClassMap<User>(map =>
                   {
                       map.AutoMap();
                       map.GetMemberMap(user => user.Role).SetRepresentation(
                                                 BsonType.String);
                   });

      I would prefer not having to do this for every enum property on collection-associated poco's which I would like to query. A fix or recommendation why its functioning as intended would be greatly appreciated

      Attachments

        Activity

          People

            craig.wilson@mongodb.com Craig Wilson
            stewartnoll Stewart Noll
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: