📜  LINQ跳过运算符

📅  最后修改于: 2021-01-06 05:19:52             🧑  作者: Mango

LINQ跳过操作员

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

LINQ跳过运算符的语法

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

C#代码

IEnumerable 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 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运算符示例

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

        }

    }

    }

输出: