
After having introduced the TakeLast operators (>)(>)(>), it makes sense to introduce their duals: the SkipLast operators.
| Name | Description | Example |
| SkipLast<TSource>(IEnumerable<TSource>) | Returns all but a specified number of contiguous elements from the end of a sequence. | int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
var lowerGrades = grades
.OrderBy(g => g)
.SkipLast(3);
Console.WriteLine("All grades except the top three are:");
foreach (int grade in lowerGrades)
{
Console.WriteLine(grade);
}
/*
This code produces the following output:
All grades except the top three are:
56
59
70
82
*/
|
| SkipLastWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) |
Returns all the elements from sequence skipping those at the end as long as the specified condition is true.
|
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
var lowerGrades = grades
.OrderBy(grade => grade)
.SkipLastWhile(grade => grade >= 80);
Console.WriteLine("All grades below 80:");
foreach (int grade in lowerGrades)
{
Console.WriteLine(grade);
}
/*
This code produces the following output:
All grades below 80:
56
59
70
*/
|
| SkipLastWhile<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>) |
Returns all the elements from sequence skipping those at the end as long as the specified condition is true.
|
int[] amounts =
{
5000,
2500,
5500,
8000,
6500,
4000,
1500,
9000
};
var query = amounts
.SkipLastWhile((amount, index) => amount > index * 1000);
foreach (int amount in query)
{
Console.WriteLine(amount);
}
/*
This code produces the following output:
9000
*/
|
You can find these (and more) operators in my CodePlex project for LINQ utilities and operators: PauloMorgado.Linq