Skip to content

Instantly share code, notes, and snippets.

@zealott
Created October 30, 2017 21:32
Show Gist options
  • Select an option

  • Save zealott/c44fb07abfd78fe39f612ec49c2a1df9 to your computer and use it in GitHub Desktop.

Select an option

Save zealott/c44fb07abfd78fe39f612ec49c2a1df9 to your computer and use it in GitHub Desktop.
Examples of Grouping inner html tags using MoreLINQ Batch method and C# GroupBy() method. I personally think the MoreLINQ Batch method is the cleanest implementation.
double blockSize = 3d;
for (int i = 0; i < Math.Ceiling(filmNavigation.Count() / blockSize) ; i++)
{
<div class="playing-wrap">
@for (int j = i * (int)blockSize; j < (i * blockSize) + (int)blockSize && j < filmNavigation.Count(); j++)
{
<div class="inner-div">j</div>
}
</div>
}
/// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/// MoreLINQ Batch Method (https://www.nuget.org/packages/morelinq/) [included by default in dss projects]
/// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
IEnumerable<ICMSElement> mainNavigationComponents = Model.Elements("MainNavigationComponent");
if (mainNavigationComponents != null && mainNavigationComponents.Any())
{
<div class="row">
@{
IEnumerable<IEnumerable<ICMSElement>> mainNavigationComponentRows = mainNavigationComponents.Batch(2);
foreach (IEnumerable<ICMSElement> row in mainNavigationComponentRows)
{
<div class="col-xs-6">
@foreach (ICMSElement comp in row)
{
<div class="icon">
.
.
.
</div>
}
</div>
}
}
</div>
}
/// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/// Using GroupBy Method
/// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
IList<ICMSElement> mainNavigationComponents = Model.Elements("MainNavigationComponent").ToList();
@if(mainNavigationComponents != null && mainNavigationComponents.Any())
{
<div class="row">
@{
// alter divisor to adjust # inner group
IEnumerable<IGrouping<int, ICMSElement>> mainNavigationComponentsRow = mainNavigationComponents.GroupBy(elt => mainNavigationComponents.IndexOf(elt) / 2);
foreach (IGrouping<int, ICMSElement> group in mainNavigationComponentsRow)
{
<div class="col-xs-6">
@{
IEnumerable<ICMSElement> mainNavigationCompGroup = group;
if(mainNavigationCompGroup != null && mainNavigationCompGroup.Any())
{
foreach(ICMSElement component in mainNavigationCompGroup)
{
<div class="icon">
.
.
.
</div>
}
}
}
</div>
}
}
</div>
}
<!-- Example HTML structure produced -->
<div class="row">
<div class="col-xs-6">
<div class="icon">
.
.
.
</div>
<div class="icon">
.
.
.
</div>
</div>
<div class="col-xs-6">
<div class="icon">
.
.
.
</div>
<div class="icon">
.
.
.
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment