In some cases it makes sense to provide multiple streams which were processed at the same time. For example, when splitting data or when providing a stream Folders
and Files
which are still related.
The following sample takes the original Persons
and splits them into odd/even streams in one preparation step.
-
-
⬇️ Result | Source ➡️
List of Data in the All stream (6)
-
Douglas (#48832)
-
Terry (#48833)
-
Neil (#48834)
-
George (#48838)
-
Raphael (#48842)
-
Ed (#48843)
List of Data in the Odd stream (2)
-
Terry (#48833)
-
Ed (#48843)
List of Data in the Even stream (4)
-
Douglas (#48832)
-
Neil (#48834)
-
George (#48838)
-
Raphael (#48842)
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade
@using System.Linq
@using ToSic.Eav.DataSources
@{
var oddEven = Kit.Data.GetSource(name: "SplitOddEven", attach: App.Data["Persons"]);
var all = oddEven.List;
var odd = oddEven["Odd"].List;
var even = oddEven["Even"].List;
}
<h3>List of Data in the All stream (@all.Count())</h3>
<ul>
@foreach (var item in AsList(all)) {
<li>
<strong>@item.EntityTitle</strong> (#@item.EntityId)
</li>
}
</ul>
<h3>List of Data in the Odd stream (@odd.Count())</h3>
<ul>
@foreach (var item in AsList(odd)) {
<li>
<strong>@item.EntityTitle</strong> (#@item.EntityId)
</li>
}
</ul>
<h3>List of Data in the Even stream (@even.Count())</h3>
<ul>
@foreach (var item in AsList(even)) {
<li>
<strong>@item.EntityTitle</strong> (#@item.EntityId)
</li>
}
</ul>
Source Code of SplitOddEven.cs
using System.Linq;
public class SplitOddEven : Custom.DataSource.DataSource16
{
public SplitOddEven(MyServices services) : base(services)
{
ProvideOut(() => TryGetIn());
ProvideOut(() => Split().Odd, name: "Odd");
ProvideOut(() => Split().Even, name: "Even");
}
private Cache Split() {
// If already cached (eg. it's retrieving Even after Odd was already retrieved), return cache
if (_cache != null)
return _cache;
// Make sure we have an In stream - otherwise return an error
var inStream = TryGetIn();
if (inStream == null) return new Cache { Odd = Error.TryGetInFailed(), Even = Error.TryGetInFailed() };
// Build cache so we don't have to re-calculate when retrieving other streams
return _cache = new Cache {
Odd = inStream.Where(e => e.EntityId % 2 == 1),
Even = inStream.Where(e => e.EntityId % 2 == 0)
};
}
private Cache _cache;
private class Cache {
public object Odd;
public object Even;
}
}