Did You Know : Using SkipWhile() feature of LINQ

by Santosh 12/4/2010 8:37:00 AM

SkipWhile() a function that bypass the elements from the sequence till the condition becomes true and will return the rest of the elements of the sequence.

Example 1 : XML data

<Products>
  <Product Name="Football"></Product>
  <Product Name="Bat"></Product>
  <Product Name="Glows"></Product>
  <Product Name="Ball"></Product>
  <Product Name="Racket"></Product>
  <Product Name="Shoes"></Product>
</Products>

Code is as follows:

Import the namespace  using System.Xml.Linq;  to use LINQ with XML.

            XDocument xdoc = new XDocument();
            xdoc = XDocument.Load("Product.xml");
            var ProductList = (from productList in (from products in xdoc.Descendants("Products")
                               select products).Descendants("Product")
                               select productList).SkipWhile(delegate(XElement prdElement) 
                                    { if (prdElement.Attribute("Name").Value == "Glows") 
                                        { return false; } else { return true; } 
                                    }).ToList();

            foreach (XElement prdElements in ProductList)
            {
                Console.WriteLine("Product Name : {0} " , prdElements.Attribute("Name").Value);
            }

Check the delegate written for SkipWhile has XElement delegate(XElement prdElement) parameter.

The parameter given depends on the output given from the Linq statement., in this case it is XElement 

Here, it will skip the below elements

  <Product Name="Football"></Product>
  <Product Name="Bat"></Product>

and will return the elements as shown below:

SkipWhile

 

Example 2 : Array of Integers

Import using System.Linq; to use LINQ features with other data types

Here the input data is array of integers as shown below:

            //Input Data
            int[] data = new int[] { 1, 11, 2, 22, 3,  33, 4, 5, 11, 12, 13, 14, 15, 16 };

            //Use Linq to get the result
            int[] calculateOutput  = data.SkipWhile(delegate(int number)
                { if (number > 14) { return false; } else { return true; } }).ToArray();

            Console.WriteLine("Following is the data:");
            foreach (int i in calculateOutput)
            {
                Console.Write("{0}, ",  i);    
            }
            Console.WriteLine("");

The delegate has a delegate(int number) parameter and it will skip till it find the first occurs of number > 14 and then it will store the rest of the data in array calculateOutput 

The output is as follows:

skipwhileInt 

 

Hope this will help using feature SkipWhile().

Thank You.Smile

Santosh



blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0