csharp 扩展方法和元组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 扩展方法和元组相关的知识,希望对你有一定的参考价值。

// Extension method definition
public static PagedCollection<TResult> ToPagedCollection<T, TResult>(
    this (IEnumerable<T> entitiesToMap, int totalCount) entitiesAndCount, // This is the important bit
    FilterParameters filterParameters,
    Func<T, TResult> map)
{
    return entitiesAndCount.entitiesToMap.ToPagedCollection(entitiesAndCount.totalCount, filterParameters?.PagingParameters?.PageSize, map);
}

// Consumption of the extension method
public async Task<PagedCollection<User>> FilterUsersAsync(FilterParameters filter)
{
    var bucket = filter.BuildSearchPredicateBucket(AspNetUserFields.Email);
    var prefetch = CreateUserRolesPrefetchPath();

    // Repository returns a tuple
    var entitiesAndCount = await _userRepository.FetchAndCountEntitiesAsync(filter, bucket, prefetch);

    // Tuple used as the main extension method argument
    return entitiesAndCount.ToPagedCollection(filter, UserMappingExtensions.ToModel);
}

以上是关于csharp 扩展方法和元组的主要内容,如果未能解决你的问题,请参考以下文章