Thursday, May 6, 2010

LINQ lambda Samples (Partitioning operator) : 'Skip' simple and Nested

Example Simple: Skip is the opposite of 'Take', where it skips the values in a collection until numeric value specified. Following simple example shows the usage if 'skip' which skips first'5' values.

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

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

Console.WriteLine("\n\nPrint num with Skip(5) ");
IEnumerable i = num.Skip(5);

foreach (int j in i)
{
Console.Write("{0} ", j);
}
}

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

Print num with Skip(5)
442 4 61 40

Example Nested : Consider the following example which first filters the collection and then apply 'Skip' to it.

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

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

Console.WriteLine("\n\nPrint num where value > 25");
IEnumerable i1 = num.Where(a => a > 25);
foreach (int j in i1)
Console.Write("{0} ", j);

Console.WriteLine("\n\nPrint num where value > 25 and skip 5");
IEnumerable i2 = num.Where(a => a > 25).Skip(5);
foreach (int j in i2)
Console.Write("{0} ", j);
}

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

Print num where value > 25
34 36 56 72 45 74 223 442 61 40

Print num where value > 25 and skip 5
74 223 442 61 40

No comments:

Post a Comment