Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Linq Skip() Method
The Skip() method in C# LINQ is used to skip a specified number of elements from the beginning of a sequence and return the remaining elements. This method is particularly useful when you need to bypass certain elements in a collection before processing the rest.
Syntax
Following is the syntax for the Skip() method −
public static IEnumerable<TSource> Skip<TSource>(
this IEnumerable<TSource> source,
int count
)
Parameters
-
source − The sequence to return elements from.
-
count − The number of elements to skip before returning the remaining elements.
Return Value
Returns an IEnumerable<TSource> that contains the elements that occur after the specified index in the input sequence.
Using Skip() with Arrays
The following example demonstrates skipping elements from an integer array after sorting in descending order −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] marks = { 80, 55, 79, 99 };
IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);
Console.WriteLine("Original array: 80, 55, 79, 99");
Console.WriteLine("After sorting descending: 99, 80, 79, 55");
Console.WriteLine("Skipped the first 2 elements:");
foreach (int res in selMarks) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Original array: 80, 55, 79, 99 After sorting descending: 99, 80, 79, 55 Skipped the first 2 elements: 79 55
Using Skip() with String Collections
The Skip() method works with any enumerable collection, including strings −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };
IEnumerable<string> skippedNames = names.Skip(2);
Console.WriteLine("Original names:");
foreach (string name in names) {
Console.WriteLine(name);
}
Console.WriteLine("\nAfter skipping first 2 names:");
foreach (string name in skippedNames) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Original names: Alice Bob Charlie David Eve After skipping first 2 names: Charlie David Eve
Skip() with Chaining Operations
You can chain Skip() with other LINQ methods for complex queries −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Skip first 3, take next 4, then filter even numbers
var result = numbers.Skip(3).Take(4).Where(x => x % 2 == 0);
Console.WriteLine("Original: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10");
Console.WriteLine("Skip(3).Take(4).Where(even):");
foreach (int num in result) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Original: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Skip(3).Take(4).Where(even): 4 6
Common Use Cases
-
Pagination − Skip records based on page size and page number.
-
Data Processing − Skip header rows in CSV files or similar data formats.
-
Result Filtering − Skip top results to get remaining items after sorting.
-
Performance Optimization − Process data in chunks by skipping processed elements.
Conclusion
The Skip() method in LINQ provides an efficient way to bypass a specified number of elements from the beginning of a sequence. It's commonly used for pagination, data processing, and result filtering, and can be easily combined with other LINQ methods for complex data operations.
