Created
October 30, 2017 21:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| /// 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> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- 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