[CSHARP-2480] The call is ambiguous between the following methods or properties: 'IMongoCollectionExtensions.FindOneAndReplace<TDocument> Created: 11/Jan/19  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Command Operations
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Charles Lindsay Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

C#


Attachments: PNG File image-2019-01-11-13-25-39-960.png    

 Description   

I'm getting an ambiguous error with the following code but only when I used the FindOneAndReplaceOptions<T> options as outlined in my picture below.

 



 Comments   
Comment by Robert Stam [ 27/Feb/19 ]

FindOneAndUpdate has the same issue.

Comment by Robert Stam [ 14/Jan/19 ]

By the way, the following also compiles:

coll.FindOneAndReplace<Dto, Dto>(c => c.Value == 0, replace, new FindOneAndReplaceOptions<Dto>() { IsUpsert = true });

but results in the other extension method being called.

The end result is the same either way.

Comment by Robert Stam [ 14/Jan/19 ]

That's unfortunate... we certainly didn't intend for this call to be ambiguous. The ambiguous call matches both of the following extension methods:

public static TDocument FindOneAndReplace<TDocument>(
    this IMongoCollection<TDocument> collection,
    Expression<Func<TDocument, bool>> filter,
    TDocument replacement,
    FindOneAndReplaceOptions<TDocument, TDocument> options = null,
    CancellationToken cancellationToken = default(CancellationToken));
 
public static TProjection FindOneAndReplace<TDocument, TProjection>(
    this IMongoCollection<TDocument> collection,
    Expression<Func<TDocument, bool>> filter,
    TDocument replacement,
    FindOneAndReplaceOptions<TDocument, TProjection> options = null, 
    CancellationToken cancellationToken = default(CancellationToken));

I'm not sure how we will fix this without making a backward breaking change, but I can offer you an immediate workaround.

To resolve the ambiguity, simply provide the generic type which will result in only one of the two methods matching, thus resolving the ambiguity:

col.FindOneAndReplace<Dto>(c => c.Value == 0, replace, new FindOneAndReplaceOptions<Dto>() { IsUpsert = true });

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