我是靠谱客的博主 震动学姐,最近开发中收集的这篇文章主要介绍C#语言中的分页转换介绍背景使用代码代码示例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

介绍

背景

使用代码

代码示例


介绍

设计分页似乎是一项微不足道的任务。它只是选择一个页面大小(或一组页面大小)并坚持下去。不使用外部数据源时,这可以完美地工作。但是,当使用不同API的返回数据列表时,您可能会遇到与您的不同的分页设计不匹配。您可以采用不同来源的页面大小,但这对您的用户来说是不透明的。因此,您现在可以保留我们自己的设计并使用PaginationConversion助手,允许您轻松地将使用分页设计的页面请求转换为所请求数据源的分页。

背景

下面的示例说明了页面大小为5发件人)和页面大小为3收件人)的两种分页设计之间的页面重叠:

该表应如下所示:

对外观设计51页的请求需要第1页和外观设计32页的前两项。

对外观设计52页的请求需要外观设计32页、第3页和第4页第一项。

等。

使用代码

PaginationConversion类帮助我们轻松地从分页设计切换到任何其他设计。它使用Convert工厂方法,要求将设计的当前页面和页面大小转换为并将设计的页面大小转换为。

using System;

/// <summary>
/// Result for the conversion from one pagination design to another.
/// </summary>
public sealed class PaginationConversion
{
    #region Properties
    
    /// <summary>
    /// First page to request in the to design.
    /// </summary>
    public int FirstPage { get; }

    /// <summary>
    /// Page size of the to design.
    /// </summary>
    public int PageSize { get; }

    /// <summary>
    /// Number of pages required from the to design to fulfill the page size of the from design.
    /// </summary>
    public int PageCount { get; }
    
    /// <summary>
    /// Number of first items to skip from the first page of the to design.
    /// </summary>    
    public int NumberOfFirstItemsToSkip { get; }
    
    #endregion //***** Properties
    
    #region Constructors
    
    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="firstPage">First page to request in the to design</param>
    /// <param name="pageSize">Page size of the to design</param>
    /// <param name="pageCount">Number of pages required from the to design to fulfill the page size of the from design</param>
    /// <param name="numberOfFirstItemsToSkip">Number of first items to skip from the first page of the to design</param>
    private PaginationConversion(int firstPage, int pageSize, int pageCount, int numberOfFirstItemsToSkip)
    {
        FirstPage = firstPage;
        PageSize = pageSize;
        PageCount = pageCount;
        NumberOfFirstItemsToSkip = numberOfFirstItemsToSkip;
    }
    
    #endregion //***** Constructors
        
    #region Methods
    
    /// <summary>
    /// Converts a request for the current page of the from pagination design to the request(s) required for the to pagination design.
    /// </summary>
    /// <param name="fromCurrentPage">Current page in the from paginition design</param>
    /// <param name="fromPageSize">Page size in the from pagination design</param>
    /// <param name="toPageSize">Page size in the to pagination design</param>
    /// <returns>Settings required for the to pagination design</returns>
    public static PaginationConversion Convert(int fromCurrentPage, int fromPageSize, int toPageSize)
    {
        //***** Calculate the (one-based) index of the first item in the from design;
        double fromFirstItemIndex = (fromCurrentPage - 1) * fromPageSize + 1;

        //***** Calculate the first page in the to design to require items from;
        var toFirstPage = (int)Math.Floor((fromFirstItemIndex - 1) / toPageSize) + 1;

        //***** Calculate the (one-based) index of the first item in the to design;
        var toFirstItemIndex = (toFirstPage - 1) * toPageSize + 1;

        //***** Calculate the first items to skip in the first page of the to design;
        var toFirstPageFirstItemsToSkip = (int) fromFirstItemIndex - toFirstItemIndex;

        //***** Calculate the required page count for the to design;
        var toPageCount = (int) Math.Ceiling((fromPageSize - (toPageSize - toFirstPageFirstItemsToSkip)) / toPageSize) + 1;

        //***** Return result;
        return new PaginationConversion(
            toFirstPage,
            toPageSize,
            toPageCount,
            toFirstPageFirstItemsToSkip);
    }    
    
    #endregion //***** Methods
}

以下伪代码演示如何实现转换结果:

public sealed class DummyRepository
{
    /// <summary>
    /// Maximum page size in the to design.
    /// </summary>
    private const toPageSize = 36;  

    /// <summary>
    /// Get the items from the to system for the current page and page size of the from design.
    /// </summary>
    /// <param name="currentPage">Current page of the from design</param>
    /// <param name="pageSize">Page size of the from design</param>
    /// <returns>Items from the to system for the current page and page size of the from design</returns>
    public List<SomeEntity> Get(int currentPage, int pageSize)
    {
        //***** Convert from design into to design;
        var pagination = PaginationConversion.Convert(sourceCurrentPage: 5, sourcePageSize: 100, targetPageSize: 36);

        //***** List of results from to system;
        var result = new List<SomeEntity>();
        var index = 0;
        while (index < pagination.PageCount)
        {
            var page = someEntityRepository.Get(pagination.FirstPage + index, pagination.PageSize);
            result.AddRange(page);             
            index++;
        }

        //***** Check if first items need to be skipped. If so, remove first items from result;
        if (pagination.NumberOfFirstItemsToSkip > 0) result.RemoveRange(0, pagination.NumberOfFirstItemsToSkip);

        //***** Check if items in result exceed page size. If so, remove last items from result;
        if (result.Count > pageSize) result.RemoveRange(result.Count - (result.Count - pageSize), result.Count - pageSize);

        //*****
        return result;
    }
}

while循环迭代页数中的页数,并收集结果列表中的项目。从源收集项目后,将删除要跳过的第一个项目(如果有)和最后一个项目(如果已超出起始设计页面大小),从而生成发件人页面。

代码示例

下面的代码示例使用页面大小为3收件人系统生成虚拟数据源。发件人系统使用的页面大小为5

public class Program
{
    public static void Main(string[] args)
    {
        //***** Helper method for retrieving pages from an enumerable;
        static List<string> GetPage(IEnumerable<string> source, int currentPage, int pageSize) => source.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

        //***** Pagination designs;
        const int fromCurrentPage = 2;
        const int fromPageSize = 5;
        const int toPageSize = 3;

        //***** Dummy data source ("to" system);
        var dataSource = new List<string>();
        for (var i=0; i < 20; i++)
            dataSource.Add($"Item {i+1}");
        
        //***** Pagination conversion;
        var pagination = PaginationConversion.Convert(fromCurrentPage, fromPageSize, toPageSize);

        //***** Iterate;
        var pages = new List<List<string>>();
        var result = new List<string>();
        var index = 0;
        while (index < pagination.PageCount)
        {
            var page = GetPage(dataSource, pagination.FirstPage + index, pagination.PageSize);
            pages.Add(page);
            result.AddRange(page);
            index++;
        }

        //***** Check if first items need to be skipped. If so, remove first items from result;
        if (pagination.NumberOfFirstItemsToSkip > 0) result.RemoveRange(0, pagination.NumberOfFirstItemsToSkip);

        //***** Check if items in result exceed page size. If so, remove last items from result;
        if (result.Count > fromPageSize) result.RemoveRange(result.Count - (result.Count - fromPageSize), result.Count - fromPageSize);
    }
}

下面的屏幕截图显示了请求的结果:

我希望这对你有意义,就像对我一样。无论如何写它很有趣:D

https://www.codeproject.com/Tips/5334616/Pagination-conversion-in-Csharp

最后

以上就是震动学姐为你收集整理的C#语言中的分页转换介绍背景使用代码代码示例的全部内容,希望文章能够帮你解决C#语言中的分页转换介绍背景使用代码代码示例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部