Skip to main content
Home  › ... Razor

LINQ Tutorials

Tutorial HomeLINQ

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

  1. Douglas Adams
  2. Terry Pratchett
  3. 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

  1. George Akerlof
  2. Raphael Müller (not an author)
  3. 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

  1. George Akerlof
  2. Raphael Müller (not an author)
<ol>
  @foreach (var person in persons.Skip(3).Take(2)) {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@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 })