X.PagedList icon indicating copy to clipboard operation
X.PagedList copied to clipboard

ambiguous method call: PagedListExtensions.ToPagedListAsync()

Open Yaevh opened this issue 1 year ago • 2 comments

Describe the bug In 8.4.7, when trying to call PagedListExtensions.ToPagedListAsync(this IQueryable<T> superset, int? pageNumber, int pageSize, CancellationToken cancellationToken), the compiler returns the following error: CS0121 The call is ambiguous between the following methods or properties: 'PagedListExtensions.ToPagedListAsync<T>(IEnumerable<T>, int, int, CancellationToken, int?)' and 'PagedListExtensions.ToPagedListAsync<T>(IQueryable<T>, int?, int, CancellationToken, int?)'

To Reproduce Consider the following code snippet:

using Microsoft.EntityFrameworkCore;
using X.PagedList;
using XPagedListAmbiguousMatchError.Data;

var dbContext = new MyDbContext();

var myQuery = dbContext.MyEntities.AsNoTracking()
    .OrderBy(x => x.Id);

var results = await myQuery.ToPagedListAsync(1, 100, CancellationToken.None); // error CS0121: ambiguous call

Additional context PagedListExtensions provide methods for both IEnumerable and IQueryable; however, in this case myQuery is of type IOrderedQueryable, which implements both IQueryable and IEnumerable. It is possible to cast myQuery to either IEnumerable or IQueryable, but it's pretty awkward, especially when ToPagedList() is used multiple times in the codebase.

The error itself was introduced in commit 8278c93c (#225), but it seems to stem from an earlier decision to make nullability of pageNumber parameter inconsistent between various ToPagedList() signatures.

Note that the two ambiguous methods in this case are https://github.com/dncuug/X.PagedList/blob/f67e889bad5271e21db09bb04cd1d6b397eac662/src/X.PagedList/PagedListExtensions.cs#L302 and https://github.com/dncuug/X.PagedList/blob/f67e889bad5271e21db09bb04cd1d6b397eac662/src/X.PagedList/PagedListExtensions.cs#L348 The subtle difference being that pageNumber parameter is optional in the second one. If I modify the signature to make pageNumber non-nullable, the error goes away.

Yaevh avatar May 17 '23 22:05 Yaevh