亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

linq中的分區(qū)操作符

 更新時間:2022年03月10日 15:18:15   作者:.NET開發(fā)菜鳥  
這篇文章介紹了linq中的分區(qū)操作符,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

一、Take操作符

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

public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count);

該方法只接受一個整數(shù),表示要返回的結(jié)果的數(shù)量。

看下面的例子:

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個連續(xù)的數(shù)據(jù)
            var q = source.Take(6);

            foreach (var item in q)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

結(jié)果:

二、TakeWhile操作符

TakeWhile操作符用于從輸入序列中返回指定數(shù)量且滿足一定條件的元素。TakeWhile方法執(zhí)行時將逐個比較序列中的每個元素是否滿足指定條件,直到碰到不符合指定條件的元素時,返回前面所有的元素組成的序列??捶椒ǖ亩x;

public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

當(dāng)TakeWhile操作符被調(diào)用時,會將source序列中的每一個元素順序傳遞給委托predicate,只有那些使得predicate返回值為true的元素才會被添加到結(jié)果序列中。要特別注意的是,當(dāng)TakeWhile操作符在查找過程中,遇到第一個返回false的元素就會立即停止執(zhí)行,跳出,不管后面還有沒有符合條件的元素,即使后面有符合條件的元素也不會要了。對于第二個擴展方法,委托里面含有一個int類型的參數(shù),該參數(shù)代表集合的下標(biāo),可以依據(jù)此進行一些據(jù)下標(biāo)操作的邏輯。

看下面的例子:

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();
        }
    }
}

結(jié)果:

三、Skip操作符

Skip操作符用于從輸入序列中跳過指定數(shù)量的元素,返回由序列中剩余的元素所組成的新序列。來看下Skip的方法定義:

public static IEnumerable<TSource> Skip<TSource>(this IEnumerable<TSource> source, int count);

可以看到,該擴展方法只接受一個整形的參數(shù),表示跳過的元素數(shù)量。

看下面的例子:

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();
        }
    }
}

結(jié)果:

四、SkipWhile操作符

SkipWhile操作符用于從輸入序列中跳過滿足一定條件指定數(shù)量的元素,與TakeWhile操作符類似。來看下SkipWhile操作符的方法定義:

public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

當(dāng)SkipWhile操作符被調(diào)用時,會將輸入序列中的元素走位參數(shù)傳遞給委托predicate,只要predicate的返回值為true,該元素就會被跳過,繼續(xù)下一個元素,直到遇到一個使predicate返回值為false的元素,此元素以及輸入序列中剩余的元素將組合一個新的序列返回。注意后面的不再判斷,直接添加到返回序列。

第二個擴展方法的委托里的參數(shù)多了個int,還是代表下標(biāo),可以根據(jù)下標(biāo)做一些邏輯處理。

看下面的例子:

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 };
            // 跳過數(shù)組中元素值小于100的元素
            var q = source.SkipWhile(i => i < 100);
            foreach (var item in q)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

結(jié)果:

五、使用Take和Skip實現(xiàn)分頁

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)
        {
            // 每頁顯示的條數(shù)
            int PageSize = 3;
            // 頁數(shù)從0開始
            int PageNum = 0;
            int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 };
            while(PageNum*PageSize<source.Length)
            {
                // 分頁
                var query = source.Skip(PageNum * PageSize).Take(PageSize);
                Console.WriteLine($"輸出第{PageNum+1}頁記錄");
                foreach (var item in query)
                {
                    Console.WriteLine(item);
                }
                PageNum++;
            }


            Console.ReadKey();
        }
    }
}

結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論