LINQ: Introducing The Skip Last Operators
After having introduced the TakeLast operators (>)(>)(>), it makes sense to introduce their duals: the SkipLast operators.
<tr>
<td valign="top"><a title="LINQ: Implementing The SkipLastWhile Operator" href="http://paulomorgado.net/en/blog/archive/2010/10/20/linq-implementing-the-skiplastwhile-operator.aspx" target="_blank">SkipLastWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)</a></td>
<td valign="top">
<p>Returns all the elements from sequence skipping those at the end as long as the specified condition is true.</p>
</td>
<td valign="top">
<pre class="code"><span style="color: blue">int</span><span style="color: black">[] grades = { </span><span style="color: #c81efa">59</span><span style="color: black">, </span><span style="color: #c81efa">82</span><span style="color: black">, </span><span style="color: #c81efa">70</span><span style="color: black">, </span><span style="color: #c81efa">56</span><span style="color: black">, </span><span style="color: #c81efa">92</span><span style="color: black">, </span><span style="color: #c81efa">98</span><span style="color: black">, </span><span style="color: #c81efa">85 </span><span style="color: black">};
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 */
<tr>
<td valign="top"><a title="LINQ: Implementing The SkipLastWhile Operator" href="http://paulomorgado.net/en/blog/archive/2010/10/20/linq-implementing-the-skiplastwhile-operator.aspx" target="_blank">SkipLastWhile<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>)</a></td>
<td valign="top">
<p>Returns all the elements from sequence skipping those at the end as long as the specified condition is true.</p>
</td>
<td valign="top">
<pre class="code"><span style="color: blue">int</span><span style="color: black">[] amounts =
{
</span><span style="color: #c81efa">5000</span><span style="color: black">,
</span><span style="color: #c81efa">2500</span><span style="color: black">,
</span><span style="color: #c81efa">5500</span><span style="color: black">,
</span><span style="color: #c81efa">8000</span><span style="color: black">,
</span><span style="color: #c81efa">6500</span><span style="color: black">,
</span><span style="color: #c81efa">4000</span><span style="color: black">,
</span><span style="color: #c81efa">1500</span><span style="color: black">,
</span><span style="color: #c81efa">9000
</span><span style="color: black">};
var query = amounts .SkipLastWhile((amount, index) => amount > index * 1000);
foreach (int amount in query) { Console.WriteLine(amount); }
/* This code produces the following output:
9000 */
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 }; |
You can find these (and more) operators in my CodePlex project for LINQ utilities and operators: PauloMorgado.Linq