Skip to main content
Home  › ... 2sxc Apps

LINQ Tutorials

Tutorial Home

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

  1. Douglas Adams (#5920)
  2. Terry Pratchett (#5921)
  3. Neil Gaiman (#5922)
  4. George Akerlof (#5926)
  5. Raphael Müller (not an author) (#5930)
  6. Ed Hardy (#5931)

<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

  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.

  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.OrderByDescending(p => p.Birthday)) {
    <li>@person.FirstName @person.LastName (@person.Birthday.ToString("d"))</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 System.Linq;

<!-- unimportant stuff, hidden -->

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

@Html.Partial("_header.cshtml")


<div class="row">
  <div class="col-lg-7">
    <h2>Simple Sorting of Persons</h2>
    <h3>OrderBy(EntityId)</h3>
  </div>
  @Html.Partial("../shared/_DefaultInfoSection.cshtml")

</div>
<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>




<hr>
<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>



<hr>
<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>



<!-- unimportant stuff, hidden -->