Learn how to leverage LINQ (Language Integrated Query) of C# to sort, filter, group content-items. This demo uses the following data in app:
This filters the authors to only find Terry.
@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.
@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>
@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>
@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 the first three authors.
@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.
@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.
@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()
Count
Count some stuff.
@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>
This example shows A-Z ordering by a property which exists on all entities: EntityId
@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
dynamic
@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.
@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>