[CSHARP-1228] Expressions used to resolve field names for enum members do not work Created: 02/Apr/15  Updated: 02/Apr/16  Resolved: 04/Apr/15

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.0
Fix Version/s: 2.0.1, 2.1

Type: Bug Priority: Major - P3
Reporter: Nick Judson Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win8.1


Attachments: Zip Archive ConsoleApplication1.zip    
Issue Links:
Related
related to CSHARP-1268 SortBy exception with enum property type Closed

 Description   

Given the class and enum:

class Person
{
	public Color FavoriteColor { get; set; }
}
 
enum Color
{
	Blue,
	SomethingElse
}

The following code snippet will not work:

Builders<Person>.IndexKeys.Ascending(x => x.FavoriteColor);
Builders<Person>.Filter.Eq(x => x.FavoriteColor, Color.Blue);
// other builders as well

Workaround

Use the string field name of the member. Even if you have mapped it to a different element name, as long as the member name is used, it will still get mapped:

Builders<Person>.IndexKeys.Ascending("FavoriteColor");



 Comments   
Comment by Githook User [ 04/Apr/15 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1228: fixed issue with enumerations in typeless field expressions not getting recognized because of a Unary conversion operator.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/cf0887859ae1986528d5b4a0eb5be2e481f07287

Comment by Githook User [ 03/Apr/15 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1228: fixed issue with enumerations in typeless field expressions not getting recognized because of a Unary conversion operator.
Branch: v2.0.x
https://github.com/mongodb/mongo-csharp-driver/commit/8803d6bf0ab49d72233720e9a63a43d23d71f589

Comment by Craig Wilson [ 02/Apr/15 ]

Thanks Nick.

I can repro this in a test. For now, a solid work-around is to use the member name as a string instead of a lambda expression. Even if you have used a different element name either via code or using the BsonElement attribute, it will still translate correctly. We've scheduled the fix for this to be 2.0.1.

Craig

Comment by Nick Judson [ 02/Apr/15 ]

Repro.

Comment by Nick Judson [ 02/Apr/15 ]

Just building out a repro.

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