Linq跳过操作符

Linq跳过操作符 首页 / LinQ入门教程 / Linq跳过操作符

在LINQ中,Skip运算符用于从列表/集合中跳过指定数量的元素,并返回剩余的元素。

LINQ跳过运算符的语法

使用LINQ Skip运算符的语法用于跳过集合中指定数量的元素,并返回集合中的其余元素。

C#代码

IEnumerable<string> result = countries.Skip(3);

From the above syntax, we are skipping the first three elements by using the "Skip (3)" and getting the remaining elements from the collection.

方法语法中的LINQ跳过操作符示例

下面是在方法语法中使用LINQ Skip运算符跳过集合中的指定元素并从集合中获取其余元素的示例。

using System;
using System. Collections;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
//create array of string type countries with the initialization
            string[] countries = { "US", "UK", "India", "Russia", "China", "Australia", "Argentina" };
/*skip method is used to with the IEnumerable to return
 the value which skip the third element of the array*/
            IEnumerable result = countries.Skip(3);
    //foreach loop is used to print the element of the array
            foreach (string s in result)
            {
                Console.WriteLine(s);
            }
                Console.ReadLine();
        }
    }
}

In the above example, we are trying to skip the first three countries. So we passed "3" as an input parameter with the help of SKIP operator. It will display the rest of the country.

无涯教程网

输出:

在LINQ中使用SKIP运算符跳过列表中的某些元素的结果如下所示。

LINQ Skip Operator

查询语法中的LINQ跳过操作符示例

C#代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {


            string[] countries = { "US", "UK", "India", "Russia", "China", "Australia", "Argentina" };

            IEnumerable result = (from x in countries select x).Skip(3);

            foreach (string s in result)

            {

                Console.WriteLine(s);

            }

            Console.ReadLine();

        }

    }

    }

输出:

LINQ Skip Operator




祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

技术与商业案例解读 -〔徐飞〕

AI技术内参 -〔洪亮劼〕

深入浅出gRPC -〔李林锋〕

消息队列高手课 -〔李玥〕

全栈工程师修炼指南 -〔熊燚(四火)〕

Django快速开发实战 -〔吕召刚〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

爆款文案修炼手册 -〔乐剑峰〕

AI大模型企业应用实战 -〔蔡超〕

好记忆不如烂笔头。留下您的足迹吧 :)