在LINQ中,Skip运算符用于从列表/集合中跳过指定数量的元素,并返回剩余的元素。
使用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 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*/ IEnumerableresult = 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运算符跳过列表中的某些元素的结果如下所示。
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" }; IEnumerableresult = (from x in countries select x).Skip(3); foreach (string s in result) { Console.WriteLine(s); } Console.ReadLine(); } } }
输出:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)