流浪のwolf

卷帝

导航

C#的Skip 和 Take 方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

// 命名空间
namespace pro01
{
    //
    internal class Program
    {
        // main 方法
         static void Main(string[] args)
        {
            // LINQ 常用的扩展方法 IEnumerable<T> 扩展方法
            // 数组 List 集合,Dictionary 字典,都是实现 IEnumerable 接口的扩展方法
            // 初始化数据
            List<Employee> list = new List<Employee>();  // 新建类型为 Employee 的集合数据
            list.Add(new Employee {Id = 1,Name = "AAA",Age = 10,Gender = true ,Salary = 1000 });
            list.Add(new Employee {Id = 2,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
            list.Add(new Employee {Id = 2,Name = "CCC",Age = 11,Gender = false ,Salary = 2000 });
            list.Add(new Employee {Id = 2,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
            list.Add(new Employee {Id = 1,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
            list.Add(new Employee {Id = 34,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
            list.Add(new Employee { Id = 3,Name = "CCC",Age = 121,Gender = true ,Salary = 7000 });
            list.Add(new Employee { Id = 3,Name = "CCC",Age = 1,Gender = true ,Salary = 3000 });
            Console.WriteLine("list 数据");
            foreach (Employee i in list)
            {
                Console.WriteLine(i);
            }
            // 限制结果集 获取部分数据  [可以用在分页上]
            //Skip(n) 跳过 n 条数据 Take(n) 获取 n 条数据
            // ge :从第 2 条开始获取 3 条数据
            var o1 = list.Skip(2).Take(3);
            Console.WriteLine(o1);
            foreach (Employee i in o1)
            {
                Console.WriteLine(i);
            }


            Console.ReadLine();
        }
    }
}

跳过 n 条数据 取出 n 条数据 

posted on 2023-07-04 00:43  流浪のwolf  阅读(54)  评论(0编辑  收藏  举报