Learn how to leverage LINQ (Language Integrated Query) of C# to sort, filter, group content-items. This demo uses the following data in app:
All our code uses some general stuff explained here:
@using System.Linq
App.Data["Books"]
dynamic
book.Name
AsList(App.Data["Books"])
IEnumerable<dynamic>
AsList(original as object)
as object
This filters the authors to only find Terry.
@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.
@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>
@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>
@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 the first three authors.
@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.
@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.
@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()
Count
Count some stuff.
@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>
This example shows A-Z ordering by a property which exists on all entities: EntityId
@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
@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.
@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>