Skip to main content
Home  › ... Razor

Data Tutorials

Tutorial HomeData

Example from Query

Queries are pre-build for things like "get all authors and the books of the author in the URL" or similar (more). These are configured in the App administration. The query AuthorsWithBooks (shown to the right) is used in this example. Below you'll see us first iterate through all the items in the Authors Stream. If an author was clicked on (which places the author-id in the url), the query will also return the current Author in the Current stream as well as the CurrentBooks of that author.

@{
  // get the query
  var query = Kit.Data.GetQuery("AuthorsWithBooks");

  // get all authors from the stream "Authors" in the query
  var allAuthors = AsList(query["Authors"]);

  // get the current author (if available) from the stream Current
  var current = AsDynamic(query["Current"].FirstOrDefault());

  // get the books of the current author (if available) from stream CurrentBook
  var books = AsList(query["CurrentBooks"]);

  // Get the current url params so we can derive from it for details links
  var urlParams = CmsContext.Page.Parameters;
}
<ul>
  @foreach (var person in allAuthors) {
    <!-- this li will have class=selected if it's the current one -->
    <li class='@(person == current ? "selected" : "")'>
      <!-- this creates a link to the current page and author=id -->
      <a href='@Link.To(parameters: urlParams.Set("authorId", person.EntityId.ToString()))'>
        @person.FirstName @person.LastName
      </a>
    </li>
  }
</ul>
@if (current != null) {
  if (Text.Has(current.Mugshot)) {
    <img loading="lazy" src="@current.Mugshot?w=100&h=100&mode=crop" width="100px" class="person float-right float-end">
  }
  <h3>Current Author: @current.FirstName @current.LastName</h3>
  <strong>Books</strong>
  <ol>
    @foreach (var book in books) {
      <li>@book.Title</li>
    }
  </ol>
} else {
  <h3>No author selected - click on an author above</h3>
}

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 ToSic.Razor.Blade;
@using System.Linq;
<!-- unimportant stuff, hidden -->


<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>  
    <h2>Example from Query</h2>
    <p>
      Queries are pre-build for things like "get all authors and the books of the author in the URL" or similar (<a href="https://2sxc.org/en/Learn/Visual-Query-Designer" target="_blank">more</a>). These are configured in the App administration. The query <code>AuthorsWithBooks</code> (shown to the right) is used in this example. Below you'll see us first iterate through all the items in the <code>Authors</code> Stream. If an author was clicked on (which places the author-id in the url), the query will also return the current Author in the <code>Current</code> stream as well as the <code>CurrentBooks</code> of that author. 
    </p>

  </div>
</div>

  @{
    // get the query
    var query = Kit.Data.GetQuery("AuthorsWithBooks");

    // get all authors from the stream "Authors" in the query
    var allAuthors = AsList(query["Authors"]);

    // get the current author (if available) from the stream Current
    var current = AsDynamic(query["Current"].FirstOrDefault());

    // get the books of the current author (if available) from stream CurrentBook
    var books = AsList(query["CurrentBooks"]);

    // Get the current url params so we can derive from it for details links
    var urlParams = CmsContext.Page.Parameters;
  }
  <ul>
    @foreach (var person in allAuthors) {
      <!-- this li will have class=selected if it's the current one -->
      <li class='@(person == current ? "selected" : "")'>
        <!-- this creates a link to the current page and author=id -->
        <a href='@Link.To(parameters: urlParams.Set("authorId", person.EntityId.ToString()))'>
          @person.FirstName @person.LastName
        </a>
      </li>
    }
  </ul>
  @if (current != null) {
    if (Text.Has(current.Mugshot)) {
      <img loading="lazy" src="@current.Mugshot?w=100&h=100&mode=crop" width="100px" class="person float-right float-end">
    }
    <h3>Current Author: @current.FirstName @current.LastName</h3>
    <strong>Books</strong>
    <ol>
      @foreach (var book in books) {
        <li>@book.Title</li>
      }
    </ol>
  } else {
    <h3>No author selected - click on an author above</h3>
  }



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })