• LINQ中分区操作符(十二)


    Linq中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作。

    一、Take操作符

    Take(int n)表示将从序列的开头返回数量为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)
            {
                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();
            }
        }
    }
    View Code

    二、TakeWhile操作符

      TakeWhile操作符用于从输入序列中返回指定数量且满足一定条件的元素。TakeWhile方法执行时将逐个比较序列中的每个元素是否满足指定条件,直到碰到不符合指定条件的元素时,返回前面所有的元素组成的序列

      当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[] source1 = new int[] { 86, 2, 77, 94, 99, 100, 5, 22, 70, 55, 100, 66, 45 };
                int[] source2 = new int[] { 101, 86, 2, 77, 94, 99, 100, 5, 22, 70, 55, 100, 66, 45 };
                string[] names = { "八戒", "悟空", "郭靖", "黄蓉", "黄药师" };
                // 返回集合中元素值小于100的序列
                var q1 = source1.TakeWhile(i => i < 100);
                var q2 = source2.TakeWhile(i => i < 100);
                var takeNames = names.TakeWhile((n, i) => n.Length == 2 && i < 3);
                foreach (var name in takeNames)
                {
                    Console.WriteLine(name);
                }
                Console.WriteLine("TakeWhile 带索引参数
    ");
    
                Console.WriteLine("*******结果一******");
                foreach (var item in q1)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("*******结果二******");
                foreach (var item in q2)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadKey();
            }
        }
    }
    View Code

    三、Skip操作符

    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[] 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();
            }
        }
    }
    View Code

    4、SkipWhile操作符

    SkipWhile操作符用于从输入序列中跳过满足一定条件指定数量的元素,与TakeWhile操作符类似

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

    五、使用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 < 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();
            }
        }
    }
    View Code

  • 相关阅读:
    python 线程通信 生产者与消费者
    python 死锁
    python 线程锁
    python 进程间通信 Queue
    python 进程 Queue
    python 阻塞模式 进程池
    python 非阻塞模式 进程池
    Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
    rest_framework序列化
    python3与django中@property详解
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12716937.html
Copyright © 2020-2023  润新知