[CSHARP-537] enum equality query on Queryable Created: 24/Jul/12  Updated: 10/Nov/12  Resolved: 10/Nov/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.5
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Stewart Noll Assignee: Craig Wilson
Resolution: Cannot Reproduce Votes: 0
Labels: Queryable, enum
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win 7, 64bit


Backwards Compatibility: 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



 Comments   
Comment by Craig Wilson [ 10/Nov/12 ]

Stewart. Since we cannot reproduce this and you haven't commented back, I'm going to close this issue. If you still feel its a problem, feel free to re-open the ticket with additional information.

Comment by Craig Wilson [ 14/Aug/12 ]

@Stewart: thanks for reporting the issue. I have created a test (code below) and cannot cause it to fail against either 1.5 or master. Would you mind ensuring that you are running at least v1.5 and perhaps update the below code to break?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
 
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.Builders;
 
namespace MongoDB.DriverUnitTests.Jira.CSharp537
{
    [TestFixture]
    public class CSharp537Tests
    {
        enum UserType
        {
            Admin,
            General,
            Affiliate
        }
 
        class User
        {
            public ObjectId Id { get; set; }
            public UserType Type { get; set; }
        }
 
        [Test]
        public void TestTypedBuildersWithSubclasses()
        {
            var db = Configuration.TestDatabase;
            var collection = db.GetCollection<User>("csharp537");
            collection.Drop();
 
            collection.Save(new User { Type = UserType.Admin });
            collection.Save(new User { Type = UserType.Affiliate });
            collection.Save(new User { Type = UserType.Affiliate });
 
            var result = from user in collection.AsQueryable()
                         where user.Type == UserType.Affiliate
                         select user;
 
            var count = result.Count();
 
            Assert.AreEqual(2, count);
        }
    }
}

Generated at Wed Feb 07 21:37:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.