[CSHARP-1246] Not partially evaluating FieldDefinitions Created: 21/Apr/15  Updated: 20/Jan/16  Resolved: 14/Jul/15

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

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

Issue Links:
Duplicate
is duplicated by CSHARP-1318 Problem evaluating variabel expressio... Closed

 Description   

Certain FieldDefinition expressions don't work, such as

var index = 1;
Builders<Person>.Update.Inc(x => x..Pets[index].Age, 1);

This will break because we have a member binding to a display class as opposed to a constant where index exists. If you are using a constant instead of a variable as an index, then there isn't a problem.

Workaround

Instead of using a lambda expression, you can use a string.

var index = 1;
var fieldName = string.Format("Pets.{0}.Age", index);
Builders<Person>.Update.Inc(fieldName, 1);

*Note that any members that have been mapped to alternate names will still get mapped. This just lacks refactoring support.



 Comments   
Comment by Githook User [ 12/Jun/15 ]

Author:

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

Message: CSHARP-1246: fixed issue where expressions including variables were not partially evaluated in a FieldDefinition.
Branch: v2.0.x
https://github.com/mongodb/mongo-csharp-driver/commit/a7ce8588ed80ca2beaad5e0c2b5f71268d69b21c

Comment by Craig Wilson [ 10/Jun/15 ]

Hi Itzhak,

I think this was mis-marked as being for 2.0.1. It was only pushed to master (which will be 2.1). We'll consider backporting to 2.0.2. My apologies for the confusion.

Craig

Comment by Itzhak Kagan [ 10/Jun/15 ]

After I took version 2.01 I run this code and got en exception:

int index = model.Index;
FilterDefinition<Post> filter = Builders<Post>.Filter.Eq(a => a.Id, model.PostId);
UpdateDefinition<Post> update = Builders<Post>.Update.Inc(a => a.Comments[index].Likes, 1);
IMongoCollection<Post> posts = blogContext.Posts;
try

{ await posts.UpdateOneAsync(filter, update); }

catch (Exception ex)

{ // log the exception }

The exception is:
"Unable to determine the serialization information for a => a.Comments.get_Item(value(M101DotNet.WebApp.Controllers.HomeController+<>c__DisplayClass29).index).Likes."} System.Exception {System.InvalidOperationException

Comment by Githook User [ 22/Apr/15 ]

Author:

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

Message: CSHARP-1246: fixed issue where expressions including variables were not partially evaluated in a FieldDefinition.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/bb61327257408ef3b40d103f4f0f5f182da2b466

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