Friday, May 7, 2010

LINQ lambda Samples (Partitioning operator) : 'SkipWhile' example

'SkipWhile' is similar to takewhile except that skipwhile will take the values from list until the condition is met.

public void SkipWhileSimple()
{
int[] num = { 34, 56, 72, 74, 223, 442, 4, 61, 40 };

Console.WriteLine("\n\nPrint num ");
foreach (int j in num)
Console.Write("{0} ", j);

Console.WriteLine("\n\nPrint num(unordered) with SkipWhile until value is not > 150 ");
IEnumerable i1 = num.SkipWhile(a => a < 150);
foreach (int j in i1)
Console.Write("{0} ", j);

Console.WriteLine("\n\nPrint num (ordered) with SkipWhile until value is not > 150 ");
IEnumerable i2 = num.OrderBy(a => a).SkipWhile(a => a < 150);
foreach (int j in i2)
Console.Write("{0} ", j);
}


OUTPUT :
Print num
34 56 72 74 223 442 4 61 40

Print num(unordered) with SkipWhile until value is not > 150
223 442 4 61 40

Print num (ordered) with SkipWhile until value is not > 150
223 442