How to use Take and Skip operators together in LINQ C#?

PHPz
Release: 2023-08-29 23:17:02
forward
617 people have browsed it

如何在 LINQ C# 中同时使用 Take 和 Skip 运算符?

The Take operator is used to return a given number of elements from an array, and array

Take, extract elements to the specified position starting from the first element in a sequence.

Example 1

is translated as: sequence.

Example 1

class Program{
   static void Main(string[] args){
      List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5,  6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 7, 8, 8 };
      System.Console.WriteLine(numbers.Count());
      var skipRes = numbers.Skip(5);
      System.Console.WriteLine(skipRes.Count());
      Console.ReadLine();
   }
}
Copy after login

Output

28
23
Copy after login

Example 2

class Program{
   static void Main(string[] args){
      List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 7, 8, 8 };
      System.Console.WriteLine(numbers.Count());
      var takeRes = numbers.Take(5);
      System.Console.WriteLine(takeRes.Count());
      Console.ReadLine();
   }
}
Copy after login

Output

28
5
Copy after login

Example 3

class Program{
   static void Main(string[] args){
      List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 7, 8, 8 };
      System.Console.WriteLine(numbers.Count());
      var takeSkipRes = numbers.Skip(10).Take(18);
      System.Console.WriteLine(takeSkipRes.Count());
      Console.ReadLine();
   }
}
Copy after login

Output

28
18
Copy after login

The above is the detailed content of How to use Take and Skip operators together in LINQ C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!