个性化阅读
专注于IT技术分析

LINQ skip操作符

点击下载

本文概述

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

LINQ跳过运算符的语法

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

C#代码

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

从以上语法中, 我们通过使用” Skip(3)”跳过前三个元素, 并从集合中获取其余元素。

方法语法中的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<string> 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();
        }
    }
}

在上面的示例中, 我们试图跳过前三个国家。因此, 我们在SKIP运算符的帮助下将” 3″作为输入参数传递。它将显示全国其他地区。

输出

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

LINQ skip运算符

查询语法中的LINQ Skip运算符示例

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<string> result = (from x in countries select x).Skip(3);

            foreach (string s in result)

            {

                Console.WriteLine(s);

            }

            Console.ReadLine();

        }

    }

    }

输出

LINQ skip运算符

赞(0)
未经允许不得转载:srcmini » LINQ skip操作符

评论 抢沙发

评论前必须登录!