#3 Use DataSources in Code - Attach/Connect Multiple Sources
Data Sources Tutorial - Basic Use
Requirements
Now we'll connect some data sources, so that the data from the first serves as input for the second.
Connect/Attach DataSources using Kit.Data
Use Kit.Data.GetAppSource
and Kit.Data.GetSource<T>
to create data sources.
This sample shows how to:
- First get all the data from the current App using
GetAppSource()
- Then get a
EntityTypeFilter
fromToSic.Eav.DataSources
- When creating the filter, also
attach
the initial app DataSource... - ...and give the filter the parameter
TypeName = "Books"
- Count the data in both DataSources
- Loop through the final items in the filter DataSource
Statistics
- App Item Count (unfiltered): 1091
- Books Item Count (filtered): 4
List of Persons from the DataSource
- Hitchhikers Guide to the Galaxy
- Good Omens
- Phishing for Phools
- The Last Continent
Connect/Attach Multiple DataSources
Often you may want to attach multiple DataSources. This sample shows how to:
- First get all the data from the current App using
GetAppSource()
- Then get a
EntityTypeFilter
fromToSic.Eav.DataSources
- When creating the filter, also
attach
the initial app DataSource... - ...and give the filter the parameter
TypeName = "Books"
- Count the data in both DataSources
- Loop through the final items in the filter DataSource
Note that you can do much more, best check out the docs.
Statistics
- App Item Count (unfiltered): 1091
- Books Item Count (filtered): 4
- Authors Item Count (filtered): 6
List of Persons from the DataSource
- Hitchhikers Guide to the Galaxy (Books)
- Good Omens (Books)
- Phishing for Phools (Books)
- The Last Continent (Books)
- Douglas (Persons)
- Terry (Persons)
- Neil (Persons)
- George (Persons)
- Raphael (Persons)
- Ed (Persons)
#3 Use DataSources in Code - Attach/Connect Multiple Sources
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 - Basic Use</h2> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <p> Now we'll connect some data sources, so that the data from the first serves as input for the second. </p> </div> </div> Connect/Attach DataSources using... <!-- unimportant stuff, hidden --> @{ // First get the root data source - the App with all data // Since we're in a Razor file it picks the current App based on the context var appDs = Kit.Data.GetAppSource(); // Now create a Type-Filter and tell it to only keep Books // * attach: Attach the App containing everything // * parameters: Set the TypeName of the filter to only keep Books var books = Kit.Data.GetSource<EntityTypeFilter>(attach: appDs, parameters: new { TypeName = "Books" }); } <h3>Statistics</h3> <ul> <li>App Item Count (unfiltered): @appDs.List.Count()</li> <li>Books Item Count (filtered): @books.List.Count()</li> </ul> <h3>List of Persons from the DataSource</h3> <ul> @foreach (var book in AsList(books)) { <li> @book.Title </li> } </ul> Connect/Attach Multiple DataSources... <!-- unimportant stuff, hidden --> @{ // First get the root data source - the App with all data var appDs2 = Kit.Data.GetAppSource(); // Now create a Type-Filter and tell it to only keep Books / Authors var books2 = Kit.Data.GetSource<EntityTypeFilter>(attach: appDs2, parameters: new { TypeName = "Books" }); var authors2 = Kit.Data.GetSource<EntityTypeFilter>(attach: appDs2, parameters: new { TypeName = "Persons" }); // The following lines are just spread for easier documentation // Usually you would write in inline var dataSourceLinks2 = books2.Link // Get the link to the books .Add(authors2.Link // Add the author2 link .Rename(inName: "Stream2" // but rename the link so it's attached as "Stream2" ) ); var merged2 = Kit.Data.GetSource<StreamMerge>(attach: dataSourceLinks2); } <h3>Statistics</h3> <ul> <li>App Item Count (unfiltered): @appDs2.List.Count()</li> <li>Books Item Count (filtered): @books2.List.Count()</li> <li>Authors Item Count (filtered): @authors2.List.Count()</li> </ul> <h3>List of Persons from the DataSource</h3> <ul> @foreach (var item in AsList(merged2)) { <li> @item.EntityTitle (@item.EntityType) </li> } </ul> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })