Introduction:

 

In this article we will learn, how to SkipWhile() method in LINQ or what is SkipWhile() method in LINQC# Asp.net MVC with example.

Description:

In LINQ, SkipWhile() method is used to skip elements in the list or collection till the specified condition is true and returns the remaining elements from collection which specified condition becomes false.

 

It returns a new collection that includes all the remaining elements once the specified condition becomes false for any element.  

 

Example of LINQ SkipWhile() Method

 

Following is the syntax of using LINQ SkipWhile() operator to skip elements in list which satisfies defined conditions and returns remaining elements from collection.

 

using System;
using System.Collections.Generic;
using System.Linq;
namespace SQLTutorialsPoint
{
    class Employee
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> Employee = new List<Employee>(){
            new Employee() {  Name = "Suresh", ID  = 1},
            new Employee() {  Name = "Sekher", ID =  2 },
              new Employee() {  Name = "Sumit", ID =  3 },
            new Employee() {  Name = "Pankaj", ID = 4 },
            new Employee() {  Name = "Rahul", ID = 5 }};
 
            var result = Employee.SkipWhile(x =>x.Name.StartsWith("S"));
 
            foreach (var item in result)
            {
                Console.WriteLine("Employee Name:"+item.Name+"  ID:" + item.ID);
            }
 
            Console.ReadLine();
        }
    }
}

 

As you see above example we have an List of employees. We want to display the employees whose name does not start with “S”. So it will start from the first employee in the List and will skip those employees whose first letter is “S” and will return the rest of the employees.

Now we will run and see the output.

 

 Use LINQ SkipWhile Operator

 

In this article we learned, how we can use LINQ skipwhile partition operator in query syntax and meth of syntax to skip some elements in list based on specified condition and return remaining elements from list / collection.

 

I hope it will help to you after reading.

 

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like