LINQ

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 t 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
Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago