日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

linq中的分區操作符_實用技巧

作者:.NET開發菜鳥 ? 更新時間: 2022-05-14 編程語言

Linq中的分區指的是在不重新排列元素的情況下,將輸入序列劃分為兩部分,然后返回其中一個部分的操作。

一、Take操作符

Take(int n)表示將從序列的開頭返回數量為n的連續元素,常用于分頁。其定義如下:

public static IEnumerable Take(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 IEnumerable TakeWhile(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 IEnumerable Skip(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 IEnumerable SkipWhile(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

欄目分類
最近更新