How to make use of both Take and Skip operator together in LINQ C#?

The Take and Skip operators in LINQ C# are powerful tools for data manipulation. The Skip operator skips over a specified number of elements from the beginning of a sequence, while the Take operator returns a specified number of elements from the beginning of a sequence.

When used together, Skip and Take enable you to implement pagination and extract specific ranges of data from collections. This combination is particularly useful for scenarios like displaying search results in pages or processing data in chunks.

Syntax

Following is the syntax for using Skip operator −

var result = collection.Skip(count);

Following is the syntax for using Take operator −

var result = collection.Take(count);

Following is the syntax for using both operators together −

var result = collection.Skip(skipCount).Take(takeCount);

How Skip and Take Work Together

Skip(3).Take(4) Operation 1 2 3 4 5 6 7 8 9 Skip (3) Take (4) Remaining Result: [4, 5, 6, 7]

Using Skip Operator

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main(string[] args) {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        Console.WriteLine("Original count: " + numbers.Count());
        
        var skipResult = numbers.Skip(5);
        Console.WriteLine("After Skip(5): " + skipResult.Count());
        Console.WriteLine("Elements: " + string.Join(", ", skipResult));
    }
}

The output of the above code is −

Original count: 10
After Skip(5): 5
Elements: 6, 7, 8, 9, 10

Using Take Operator

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main(string[] args) {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        Console.WriteLine("Original count: " + numbers.Count());
        
        var takeResult = numbers.Take(5);
        Console.WriteLine("After Take(5): " + takeResult.Count());
        Console.WriteLine("Elements: " + string.Join(", ", takeResult));
    }
}

The output of the above code is −

Original count: 10
After Take(5): 5
Elements: 1, 2, 3, 4, 5

Using Skip and Take Together

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main(string[] args) {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
        Console.WriteLine("Original count: " + numbers.Count());
        
        var combinedResult = numbers.Skip(5).Take(7);
        Console.WriteLine("After Skip(5).Take(7): " + combinedResult.Count());
        Console.WriteLine("Elements: " + string.Join(", ", combinedResult));
        
        // Pagination example
        int pageSize = 3;
        int pageNumber = 2; // 0-based indexing
        var pageResult = numbers.Skip(pageNumber * pageSize).Take(pageSize);
        Console.WriteLine("Page " + pageNumber + " (size " + pageSize + "): " + string.Join(", ", pageResult));
    }
}

The output of the above code is −

Original count: 15
After Skip(5).Take(7): 7
Elements: 6, 7, 8, 9, 10, 11, 12
Page 2 (size 3): 7, 8, 9

Common Use Cases

  • Pagination: Displaying data in pages by skipping previous pages and taking a specific page size.

  • Data Processing: Processing large datasets in chunks by skipping processed items and taking the next batch.

  • Range Selection: Extracting a specific range of elements from a collection.

Conclusion

The combination of Skip and Take operators in LINQ C# provides an efficient way to implement pagination and extract specific ranges of data from collections. This approach is essential for handling large datasets and creating user-friendly data presentation interfaces.

Updated on: 2026-03-17T07:04:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements