網站首頁 編程語言 正文
Linq中的分區指的是在不重新排列元素的情況下,將輸入序列劃分為兩部分,然后返回其中一個部分的操作。
一、Take操作符
Take(int n)表示將從序列的開頭返回數量為n的連續元素,常用于分頁。其定義如下:
public static IEnumerableTake (this IEnumerable source, int count);
該方法只接受一個整數,表示要返回的結果的數量。
看下面的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PartitionOperation { class Program { static void Main(string[] args) { int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; // 返回6個連續的數據 var q = source.Take(6); foreach (var item in q) { Console.WriteLine(item); } Console.ReadKey(); } } }
結果:
二、TakeWhile操作符
TakeWhile操作符用于從輸入序列中返回指定數量且滿足一定條件的元素。TakeWhile方法執行時將逐個比較序列中的每個元素是否滿足指定條件,直到碰到不符合指定條件的元素時,返回前面所有的元素組成的序列。看方法的定義;
public static IEnumerableTakeWhile (this IEnumerable source, Func predicate); public static IEnumerable TakeWhile (this IEnumerable source, Func predicate);
當TakeWhile操作符被調用時,會將source序列中的每一個元素順序傳遞給委托predicate,只有那些使得predicate返回值為true的元素才會被添加到結果序列中。要特別注意的是,當TakeWhile操作符在查找過程中,遇到第一個返回false的元素就會立即停止執行,跳出,不管后面還有沒有符合條件的元素,即使后面有符合條件的元素也不會要了。對于第二個擴展方法,委托里面含有一個int類型的參數,該參數代表集合的下標,可以依據此進行一些據下標操作的邏輯。
看下面的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PartitionOperation { class Program { static void Main(string[] args) { int[] source = new int[] { 86, 2, 77, 94, 99, 100, 5, 22, 70, 55, 100, 66, 45 }; // 返回集合中元素值小于100的序列 var q = source.TakeWhile(i => i < 100); foreach (var item in q) { Console.WriteLine(item); } Console.ReadKey(); } } }
結果:
三、Skip操作符
Skip操作符用于從輸入序列中跳過指定數量的元素,返回由序列中剩余的元素所組成的新序列。來看下Skip的方法定義:
public static IEnumerableSkip (this IEnumerable source, int count);
可以看到,該擴展方法只接受一個整形的參數,表示跳過的元素數量。
看下面的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PartitionOperation { class Program { static void Main(string[] args) { int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; // 跳過5個元素 var q = source.Skip(5); foreach (var item in q) { Console.WriteLine(item); } Console.ReadKey(); } } }
結果:
四、SkipWhile操作符
SkipWhile操作符用于從輸入序列中跳過滿足一定條件指定數量的元素,與TakeWhile操作符類似。來看下SkipWhile操作符的方法定義:
public static IEnumerableSkipWhile (this IEnumerable source, Func predicate); public static IEnumerable SkipWhile (this IEnumerable source, Func predicate);
當SkipWhile操作符被調用時,會將輸入序列中的元素走位參數傳遞給委托predicate,只要predicate的返回值為true,該元素就會被跳過,繼續下一個元素,直到遇到一個使predicate返回值為false的元素,此元素以及輸入序列中剩余的元素將組合一個新的序列返回。注意后面的不再判斷,直接添加到返回序列。
第二個擴展方法的委托里的參數多了個int,還是代表下標,可以根據下標做一些邏輯處理。
看下面的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PartitionOperation { class Program { static void Main(string[] args) { int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; // 跳過數組中元素值小于100的元素 var q = source.SkipWhile(i => i < 100); foreach (var item in q) { Console.WriteLine(item); } Console.ReadKey(); } } }
結果:
五、使用Take和Skip實現分頁
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PartitionOperation { class Program { static void Main(string[] args) { // 每頁顯示的條數 int PageSize = 3; // 頁數從0開始 int PageNum = 0; int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; while(PageNum*PageSize
結果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9315726.html
相關推薦
- 2022-07-12 手把手教你用Redis?實現點贊功能并且與數據庫同步_Redis
- 2022-04-01 exception occurred during ITK-SNAP startup itk-sn
- 2022-05-23 高效的數據同步工具DataX的使用及實現示例_數據庫其它
- 2024-04-03 Validator工具驗證類,區分添加,刪除,修改
- 2022-05-13 e engine “node“ is incompatible with this module.
- 2023-07-13 react 非授控組件和授控組件的區別
- 2022-11-14 C++資源管理操作方法詳解_C 語言
- 2022-10-02 Blender?Python編程快速入門教程_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支