Details
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 ![]()