Thanks for your reaction, but it was a bit to technical for me.
I asked ChatGPT the same question and it came up with this:
var items = Model.Value<IEnumerable<IPublishedElement>>("partners");
if (items != null) {
var setsOfFour = items.Chunk(4);
foreach (var set in setsOfFour){
<div class="set-of-four">
@foreach (var item in set){
<p>@item.Name</p>
}
</div>
}
}
No problem, sorry I wasn't clearer!, glad you got it working!
Basically, InGroupsOf is an extension method provided by Umbraco to help people do what you have done without having to know about things like 'chunk'!
so your example above would be:
var items = Model.Value<IEnumerable<IPublishedElement>>("partners");
if (items != null) {
foreach (var set in items.InGroupsOf(4)){
<div class="set-of-four">
@foreach (var item in set){
<p>@item.Name</p>
}
</div>
}
}
It's just meant to provide a sugary syntax that is more descriptive about what is going on, but if you are familiar with the raw Linq method, then it's completely fine to do it that way!
How to split a foreach loop in groups of four items?
How can i split a foreach in groups of (max.) four? It is a list from a nested content
Hi Peter
I wonder if you can make use of the InGroupsOf extension method that Umbraco has in the Umbraco.Extensions namespace...
https://github.com/umbraco/Umbraco-CMS/blob/7df5c9c212185bf50f27aa348e3c3698cc97af57/src/Umbraco.Core/Extensions/EnumerableExtensions.cs#L41
So you can do
Regards
Marc
Thanks for your reaction, but it was a bit to technical for me. I asked ChatGPT the same question and it came up with this:
It worked for me.
Hi Peter
No problem, sorry I wasn't clearer!, glad you got it working!
Basically, InGroupsOf is an extension method provided by Umbraco to help people do what you have done without having to know about things like 'chunk'!
so your example above would be:
It's just meant to provide a sugary syntax that is more descriptive about what is going on, but if you are familiar with the raw Linq method, then it's completely fine to do it that way!
regards
Marc
is working on a reply...