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.
- is duplicated by
-
CSHARP-1318 Problem evaluating variabel expression in lambda expressions
- Closed