27 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace AGSS.Utilities;
public static class LINQ
{
/// <summary>
/// 从给定的序列中分页获取元素。
/// </summary>
/// <typeparam name="T">序列中的元素类型。</typeparam>
/// <param name="source">要从中分页的源序列。</param>
/// <param name="pageIndex">请求的页码从0开始。</param>
/// <param name="pageSize">每页包含的元素数量。</param>
/// <returns>返回指定页码和页大小对应的子序列。</returns>
/// <exception cref="ArgumentNullException">如果source为null。</exception>
/// <exception cref="ArgumentOutOfRangeException">如果pageIndex是负数或者pageSize是非正数。</exception>
public static IEnumerable<T> Paginate<T>(this IEnumerable<T> source, int pageIndex, int pageSize)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (pageIndex < 0) throw new ArgumentOutOfRangeException(nameof(pageIndex), "Page index must be non-negative.");
if (pageSize <= 0) throw new ArgumentOutOfRangeException(nameof(pageSize), "Page size must be positive.");
return source.Skip(pageIndex * pageSize).Take(pageSize);
}
}