Skip to main content
Home  › ... Razor

LINQ Tutorial (Language INtegrated Query)

Tutorial HomeLINQ

LINQ Basics

Learn how to leverage LINQ (Language Integrated Query) of C# to sort, filter, group content-items. This demo uses the following data in app:

  • Persons - various people who are used in the data. A person can also have one or many favorite books.
  • Books - books people wrote or contributed to. Books have authors and

Some notes before we start

All our code uses some general stuff explained here:

  • to enable LINQ commands we always need:
    @using System.Linq
  • most of the code starts by retrieving a list of Books and Authors. This is done using:
    App.Data["Books"]
  • Since we want to use dynamic types (which lets us write things like book.Name, we usually wrap it with:
    AsList(App.Data["Books"])
  • The compiler often can't guess object types we are using, we often need to cast lists to:
    IEnumerable<dynamic>
    The easiest way is to just run it through AsList(original as object).
    The as object part necessary because of limitations in Razor.
The samples can differ based on your Razor base class or if you're running an old version.
Switch to Typed (2sxc 16+) Selected: Dynamic (Razor14 or below)

Simple Where(...) and Any()

This filters the authors to only find Terry.

⬇️ Result | Source ➡️

  1. Terry Pratchett
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  // Initial Code
  var persons = AsList(App.Data["Persons"]);
}
<ol>
  @foreach (var person in persons
  .Where(p => p.FirstName == "Terry")) 
  {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

This filters the authors with long first names.

⬇️ Result | Source ➡️

  1. Douglas Adams
  2. George Akerlof
  3. Raphael Müller (not an author)
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  // Initial Code
  var persons = AsList(App.Data["Persons"]);
}  
<ol>
  @foreach (var person in persons
    .Where(p => p.FirstName.Length > 5)) 
  {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

This filters the authors with long first names.

⬇️ Result | Source ➡️

  1. Persons with 5-char names or more: True
  2. Persons with 10-char names or more: False
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  // Initial Code
  var persons = AsList(App.Data["Persons"]);
}
<ol>
  <li>
    Persons with 5-char names or more: 
    @persons.Any(p => p.FirstName.Length > 5)
  </li>
  <li>
    Persons with 10-char names or more: 
    @persons.Any(p => p.FirstName.Length > 10)
  </li>
</ol>

Simple First() and Last()

This filters the authors with long first names.

⬇️ Result | Source ➡️

  1. First: Douglas
  2. Last: Ed
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  // Initial Code
  var persons = AsList(App.Data["Persons"]);
}
<ol>
  <li>
    First: @persons.First().FirstName
  </li>
  <li>
    Last: @persons.Last().FirstName
  </li>
</ol>

Take() / Skip()

Take the first three authors.

⬇️ Result | Source ➡️

  1. Douglas Adams
  2. Terry Pratchett
  3. Neil Gaiman
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  var persons = AsList(App.Data["Persons"]);
  var books = AsList(App.Data["Books"]);
}
<ol>
  @foreach (var person in persons.Take(3)) {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

Skip the first three authors.

⬇️ Result | Source ➡️

  1. George Akerlof
  2. Raphael Müller (not an author)
  3. Ed Hardy
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  var persons = AsList(App.Data["Persons"]);
  var books = AsList(App.Data["Books"]);
}
<ol>
  @foreach (var person in persons.Skip(3)) {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

Skip the first three authors, then take 2.

⬇️ Result | Source ➡️

  1. George Akerlof
  2. Raphael Müller (not an author)
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  var persons = AsList(App.Data["Persons"]);
  var books = AsList(App.Data["Books"]);
}
<ol>
  @foreach (var person in persons.Skip(3).Take(2)) {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

Count() and Count

Count some stuff.

⬇️ Result | Source ➡️

  1. All Persons: 6
  2. All Books: 4
  3. Books with Illustrators: System.Linq.Enumerable+WhereEnumerableIterator`1[System.Object]
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
// Initial Code
  var persons = AsList(App.Data["Persons"]);
  var books = AsList(App.Data["Books"]);
}
<ol>
  <li>
    All Persons: @persons.Count()
  </li>
  <li>
    All Books: @books.Count()
  </li>
  <li>
    Books with Illustrators: @books.Where(b => (b.Illustrators.Count > 0).Count())
  </li>
</ol>

Simple Sorting of Persons

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

⬇️ Result | Source ➡️

  1. Douglas Adams (#48832)
  2. Terry Pratchett (#48833)
  3. Neil Gaiman (#48834)
  4. George Akerlof (#48838)
  5. Raphael Müller (not an author) (#48842)
  6. Ed Hardy (#48843)
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  var persons = AsList(App.Data["Persons"]);
}
<ol>
  @foreach (var person in persons
    .OrderBy(p => p.EntityId))
  {
    <li>@person.FirstName @person.LastName 
      (#@person.EntityId)
    </li>
  }
</ol>

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

⬇️ Result | Source ➡️

  1. Douglas Adams
  2. Ed Hardy
  3. George Akerlof
  4. Neil Gaiman
  5. Raphael Müller (not an author)
  6. Terry Pratchett
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  var persons = AsList(App.Data["Persons"]);
}
<ol>
  @foreach (var person in persons
    .OrderBy(p => p.FirstName))
  {
    <li>@person.FirstName @person.LastName</li>
  }
</ol>

This example shows Z-A ordering by a property.

⬇️ Result | Source ➡️

  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)
@inherits Custom.Hybrid.Razor14
@using System.Linq

@{
  var persons = AsList(App.Data["Persons"]);
}
<ol>
  @foreach (var person in persons
    .OrderByDescending(p => p.Birthday)) 
  {
    <li>@person.FirstName @person.LastName 
      (@person.Birthday.ToString("d"))
    </li>
  }
</ol>