LINQ Partition Operators

In Linq, partition operators are used to dividing the collection/list into two parts. There are various partition operators like Take(), TakeWhile(), Skip(), SkipWhile(). All have its unique use and we can use it in our demand basis.

Linq Take Operator

It is used to get the specified number of elements from the list. It returns the specified number of elements from the list and ignores the rest.

Syntax:

IEnumerable<string> data = state.Take(3);

Example:

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

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] state = { "Gujarat", "Maharashtra", "Rajasthan", "Kerala", "Andhra Pradesh", "Uttar Pradesh" };
            IEnumerable<string> data = state.Take(3);
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

Output:

Gujarat
Maharashtra
Rajasthan

Linq TakeWhile operator

It is used to take the data if the condition specified in it is true.

Syntax:

IEnumerable<string> data = state.TakeWhile(m => m.Contains("t"));

Example:

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

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] state = { "Gujarat", "Maharashtra", "Rajasthan", "Kerala", "Andhra Pradesh", "Uttar Pradesh" };
            IEnumerable<string> data = state.TakeWhile(m => m.Contains("t"));
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

Here, we will only take the string which contains the letter in it.

Output:

Gujarat
Maharashtra
Rajasthan

Linq Skip Operator

It is used to skip the specified number of elements from the list or collection and returns the remaining elements from the list or collection.

Syntax:

IEnumerable<string> data = state.Skip(2);

Example:

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

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] state = { "Gujarat", "Maharashtra", "Rajasthan", "Kerala", "Andhra Pradesh", "Uttar Pradesh" };
            IEnumerable<string> data = state.Skip(2);
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

Output:

Rajasthan
Kerala
Andhra Pradesh
Uttar Pradesh

Linq SkipWhile Operator

It is used to skip the elements from the list or collection if it meets the specified condition and returns the remaining elements.

Syntax:

IEnumerable<string> data = state.SkipWhile(m => m.Contains("t"));

Example:

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

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] state = { "Gujarat", "Maharashtra", "Rajasthan", "Kerala", "Andhra Pradesh", "Uttar Pradesh" };
            IEnumerable<string> data = state.SkipWhile(m => m.Contains("t"));
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

Output:

Kerala
Andhra Pradesh
Uttar Pradesh

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories