App


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.GetStream("Books")
The samples can differ based on your Razor base class or if you're running an old version.
Selected: Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)

Simple Where(...) and Any()

This filters the authors to only find Terry.

⬇️ Result | Source ➡️

  1. Terry Pratchett
@inherits Custom.Hybrid.RazorTyped
@using System.Linq;

@{
  // Initial Code
  var persons = AsItems(App.Data.GetStream("Persons"));
}
<ol>
  @foreach (var person in persons
  .Where(p => p.String("FirstName") == "Terry")) 
  {
    <li>@person.Get("FirstName") @person.Get("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.RazorTyped
@using System.Linq;

@{
  // Initial Code
  var persons = AsItems(App.Data.GetStream("Persons"));
}  
<ol>
  @foreach (var person in persons
  .Where(p => p.String("FirstName").Length > 5)) 
  {
    <li>@person.Get("FirstName") @person.Get("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.RazorTyped
@using System.Linq;

@{
  // Initial Code
  var persons = AsItems(App.Data.GetStream("Persons"));
}
<ol>
  <li>
    Persons with 5-char names or more: 
    @persons.Any(p => p.String("FirstName").Length > 5)
  </li>
  <li>
    Persons with 10-char names or more: 
    @persons.Any(p => p.String("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.RazorTyped
@using System.Linq;

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

Take() / Skip()

Take the first three authors.

⬇️ Result | Source ➡️

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

@{
  var persons = AsItems(App.Data.GetStream("Persons"));
  var books = AsItems(App.Data.GetStream("Books"));
}
<ol>
  @foreach (var person in persons.Take(3)) {
    <li>@person.Get("FirstName") @person.Get("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.RazorTyped
@using System.Linq;

@{
  var persons = AsItems(App.Data.GetStream("Persons"));
  var books = AsItems(App.Data.GetStream("Books"));
}
<ol>
  @foreach (var person in persons.Skip(3)) {
    <li>@person.Get("FirstName") @person.Get("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.RazorTyped
@using System.Linq;

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

Count() and Count

Count some stuff.

⬇️ Result | Source ➡️

  1. All Persons: 6
  2. All Books: 4
  3. Books with Illustrators: 1)
@inherits Custom.Hybrid.RazorTyped
@using System.Linq;

@{
// Initial Code
  var persons = AsItems(App.Data.GetStream("Persons"));
  var books = AsItems(App.Data.GetStream("Books"));
}
<ol>
  <li>
    All Persons: @persons.Count()
  </li>
  <li>
    All Books: @books.Count()
  </li>
  <li>
    Books with Illustrators:
    @books.Where(b => b.Children("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 (#46284)
  2. Terry Pratchett (#46285)
  3. Neil Gaiman (#46286)
  4. George Akerlof (#46290)
  5. Raphael Müller (not an author) (#46294)
  6. Ed Hardy (#46295)
@inherits Custom.Hybrid.RazorTyped
@using System.Linq;

@{
  var persons = AsItems(App.Data.GetStream("Persons"));
}
<ol>
  @foreach (var person in persons
    .OrderBy(p => p.Id)) 
  {
    <li>@person.Get("FirstName") 
      @person.Get("LastName") (#@person.Id)
    </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.RazorTyped
@using System.Linq;

@{
  var persons = AsItems(App.Data.GetStream("Persons"));
}
<ol>
  @foreach (var person in persons
  .OrderBy(p => p.String("FirstName"))) 
  {
    <li>@person.Get("FirstName") @person.Get("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.RazorTyped
@using System.Linq;

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