Data Sources Tutorial - Use GetQuery
Kit.Data.GetQuery()
should be used to retrieve a query.
It also allows us to pass in parameters if the query supports it.
The following demos all use this query.
TODO: PIC of query
Get a Query as DataSource using Kit.Data.GetQuery
Use Kit.Data.GetQuery("QueryName")
to get a Query DataSource.
This first sample does not use parameters so the sorting happens on whatever was set as default.
List of Persons from the DataSource
- Douglas Adams
- George Akerlof
- Neil Gaiman
- Ed Hardy
- Raphael Müller (not an author)
- Terry Pratchett
Give parameters to Query
This will also provide parameters to change how it is sorted. Note that these parameters only work, because the Query expects these.
List of Persons from the DataSource
- Terry Pratchett
- Raphael Müller (not an author)
- Neil Gaiman
- George Akerlof
- Ed Hardy
- Douglas Adams
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; @using ToSic.Eav.DataSources; <!-- unimportant stuff, hidden --> <h2>Data Sources Tutorial - Use GetQuery</h2> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <p> <code>Kit.Data.GetQuery()</code> should be used to retrieve a query. It also allows us to pass in parameters if the query supports it. The following demos all use this query. TODO: PIC of query </p> </div> </div> Get a Query as DataSource using... <!-- unimportant stuff, hidden --> @{ var queryNoParams = Kit.Data.GetQuery("GetQueryDemoAuthorsSorted"); } <h3>List of Persons from the DataSource</h3> <ul> @foreach (var person in AsList(queryNoParams)) { <li> @person.FirstName @person.LastName </li> } </ul> Give parameters to Query This will also... <!-- unimportant stuff, hidden --> @{ var queryFirstNameDesc = Kit.Data.GetQuery("GetQueryDemoAuthorsSorted", parameters: new { Sort = "Desc", Field = "FirstName" }); } <h3>List of Persons from the DataSource</h3> <ul> @foreach (var person in AsList(queryFirstNameDesc)) { <li> @person.FirstName @person.LastName </li> } </ul> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })