Difference
between Skip() and SkipWhile() extension methods in LINQ
-
S.No Skip() SkipWhile() 1 SkipWhile() continues to skip the elements as long as the input condition is true. Once condition turns false it will return all remaining elements.
2.Signature
of Skip ():
public
static IEnumerable Skip( this
IEnumerable source, int count);
3.Example:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Skip(3);
In this example, the Skip method will skip the first 3 items from the collection and return the remaining items as an IEnumerable collection.
So the output of this example will be 3, 9, 8, 6, 7, 2 and 0.
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Skip(3);
In this example, the Skip method will skip the first 3 items from the collection and return the remaining items as an IEnumerable collection.
So the output of this example will be 3, 9, 8, 6, 7, 2 and 0.
2a.Signature
of SkipWhile ():
public
static IEnumerable SkipWhile( this
IEnumerable source, =unc
predicate);
2b.Another
signature for overriding:
public
static IEnumerable SkipWhile( this
IEnumerable source, Func
predicate);
3a.Example
I:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7 };
var remaining = numbers.SkipWhile(n => n < 9);
In this example the SkipWhile operator will skip the items from the starting position until the conditions fails. Once the condition has failed then it will return the remaining items as an IEnumerable collection.
So the output for this example is 9, 8, 6 and 7.
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7 };
var remaining = numbers.SkipWhile(n => n < 9);
In this example the SkipWhile operator will skip the items from the starting position until the conditions fails. Once the condition has failed then it will return the remaining items as an IEnumerable collection.
So the output for this example is 9, 8, 6 and 7.
3b.Example
II:
int[] numbers = { 5, 4, 1, 3, 9, 8, 2, 0 };
var indexed = numbers.SkipWhile((n, index) => n > index);
In this example the SkipWhile method will skip the items greater than their positions.
So in this example the output is 1, 3, 9, 8, 2 and 0. Because the position of the 1 is 2 so here the condition fails.
int[] numbers = { 5, 4, 1, 3, 9, 8, 2, 0 };
var indexed = numbers.SkipWhile((n, index) => n > index);
In this example the SkipWhile method will skip the items greater than their positions.
So in this example the output is 1, 3, 9, 8, 2 and 0. Because the position of the 1 is 2 so here the condition fails.
Great Article...
ReplyDeleteOnline DotNet Training
.Net Online Training
Dot Net Training in Chennai
Online LINQ
Training
LINQ Training
Online LINQ Training from India
LINQ Training in Chennai
IT Training in Chennai
"Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
ReplyDelete"