Skip to main content
Home  › ... Razor

LINQ Tutorials

Tutorial HomeLINQ

Simple Sorting of Persons

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"]);
}

OrderBy(EntityId)

This example shows A-Z ordering by a property which exists on all entities: EntityId

Output

  1. Douglas Adams (#33125)
  2. Terry Pratchett (#33126)
  3. Neil Gaiman (#33127)
  4. George Akerlof (#33131)
  5. Raphael Müller (not an author) (#33135)
  6. Ed Hardy (#33136)
<ol>
  @foreach (var person in persons.OrderBy(p => p.EntityId)) {
    <li>@person.FirstName @person.LastName (#@person.EntityId)</li>
  }
</ol>

OrderBy(FirstName)

This example shows A-Z ordering by a property which exists only on Person-entities. This is simple with dynamic objects

Output

  1. Douglas Adams
  2. Ed Hardy
  3. George Akerlof
  4. Neil Gaiman
  5. Raphael Müller (not an author)
  6. Terry Pratchett
<ol>
  @foreach (var person in persons.OrderBy(p => p.FirstName)) {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

OrderByDescending(Birthday)

This example shows Z-A ordering by a property.

Output

  1. Raphael Müller (not an author) (3/1/2000)
  2. Neil Gaiman (11/10/1960)
  3. Douglas Adams (3/11/1952)
  4. Terry Pratchett (4/28/1948)
  5. Ed Hardy (1/1/1945)
  6. George Akerlof (6/17/1940)
<ol>
  @foreach (var person in persons.OrderBy(p => p.FirstName)) {
    <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>Simple Sorting of Persons</h2>
  </div>

  @{
    var persons = AsList(App.Data["Persons"]);
  }



</div>
<h3>OrderBy(EntityId)</h3>
<p>This example shows A-Z ordering by a property which exists on all entities: EntityId</p>

  <ol>
    @foreach (var person in persons.OrderBy(p => p.EntityId)) {
      <li>@person.FirstName @person.LastName (#@person.EntityId)</li>
    }
  </ol>


<h3>OrderBy(FirstName)</h3>
<p>This example shows A-Z ordering by a property which exists only on Person-entities. This is simple with <code>dynamic</code> objects</p>

  <ol>
    @foreach (var person in persons.OrderBy(p => p.FirstName)) {
      <li>@person.FirstName @person.LastName</li>
    }
  </ol>



<h3>OrderByDescending(Birthday)</h3>
<p>This example shows Z-A ordering by a property.</p>

  <ol>
    @foreach (var person in persons.OrderByDescending(p => p.Birthday)) {
      <li>@person.FirstName @person.LastName (@person.Birthday.ToString("d"))</li>
    }
  </ol>



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })