#4 Take() / Skip() items in a list
Initial Code
The following code runs at the beginning and creates some variables/services used in the following samples.
@{
var persons = AsList(App.Data["Persons"]);
var books = AsList(App.Data["Books"]);
}
Take(3)
Take the first three authors.
Output
- Douglas Adams
- Terry Pratchett
- Neil Gaiman
<ol>
@foreach (var person in persons.Take(3)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
Skip(3)
Skip the first three authors.
Output
- George Akerlof
- Raphael Müller (not an author)
- Ed Hardy
<ol>
@foreach (var person in persons.Skip(3)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
Skip(3).Take(2)
Skip the first three authors, then take 2.
Output
- George Akerlof
- Raphael Müller (not an author)
<ol>
@foreach (var person in persons.Skip(3).Take(2)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
#4 Take() / Skip() items in a list
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
@using System.Linq;
<!-- unimportant stuff, hidden -->
<div @Sys.PageParts.InfoWrapper()>
@Html.Partial("../shared/DefaultInfoSection.cshtml")
<div @Sys.PageParts.InfoIntro()>
<h2>Take() / Skip()</h2>
</div>
</div>
@{
var persons = AsList(App.Data["Persons"]);
var books = AsList(App.Data["Books"]);
}
<h3>Take(3)</h3>
<p>Take the first three authors.</p>
<ol>
@foreach (var person in persons.Take(3)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
<h3>Skip(3)</h3>
<p>Skip the first three authors.</p>
<ol>
@foreach (var person in persons.Skip(3)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
<h3>Skip(3).Take(2)</h3>
<p>Skip the first three authors, then take 2.</p>
<ol>
@foreach (var person in persons.Skip(3).Take(2)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })