C# LINQ3 array positional operator no longer works?

I have been using the new C# beta driver with LINQ3 support, which is fantastic because it supports much more complex queries than the previous one. But I also noticed that being able to use -1 as a positional operator no longer appears to work when calling UpdateOneAsync (and probably UpdateManyAsync and its synchronous cousins).

For example, the following code ran just fine in LINQ2:

await collection.UpdateOneAsync(l => l.Id == another.Id && l.AnArrayMember.Any(l => l.Id == anArrayId),
                    Builders<ModelClass>.Update.Set(l => l.AnArrayMember.ElementAt(-1).Deleted, true));

This now fails with the error Cannot create field '-1' in element [...], where before it successfully translated it into AnArrayMember.$.Deleted and the Set command completed fine.

Has the use of this changed? Is there another way to perform this operation with LINQ3?

Thanks in advance for your help!

Hi, Renn,

Thank you for reaching out to us. This appears to be an unintentional difference in behaviour between LINQ2 and LINQ3. In LINQ2, we used -1 as a magic number to indicate the use of the positional operator, but this was not carried over to LINQ3. I have created CSHARP-4079 to track this bug.

We appreciate you reporting this issue. Please follow CSHARP-4079 for updates.

Sincerely,
James

1 Like

Hi James, greatly appreciated. I’ve found a work-around by not using LINQ expressions in the Set operation, but of course a strongly-typed LINQ-compatible way of doing this would be preferable.

Thanks again for looking into this and hopefully this will be resolved in future builds.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.