Search code examples
c#loopsumbraco

Looping multiple items but only loop unique ones


I currently have a loop that loops different tags which are dynamically loaded from Umbraco.

This is the result of my loop: enter image description here

Code of the loop:

@{ var Artikelen = Model.Content.Children.Where(NieuwsTemplate => NieuwsTemplate.DocumentTypeAlias == "newsDetail"); }
@foreach (var tag in @MainTag.Children){
    foreach (var NewsArticleTags in Artikelen){
        var DifferentTags = NewsArticleTags.GetPropertyValue<IEnumerable<IPublishedContent>>("themeTags");
        foreach(var TagNames in DifferentTags){
            if(@TagNames.Name == @tag.Name){
                <center><a class="ajaxFilter" data-catId="@tag.Id">@tag.Name</a></center>
            }
        }   
    }    
} 

I decided to add a bool so it only loops 1 of each. I obviously messed up because it only loops the very first tag. Where am I suppose to put the bool so it loops each tag only once? 1 "Gamification" 1 "VR / AR" and 1 "Leren Programmeren"

Here is the code with the bool added:

@{ var Artikelen = Model.Content.Children.Where(NieuwsTemplate => NieuwsTemplate.DocumentTypeAlias == "newsDetail"); 
bool ShowTag = false;}

@foreach (var tag in @MainTag.Children){
    foreach (var NewsArticleTags in Artikelen){
        var DifferentTags = NewsArticleTags.GetPropertyValue<IEnumerable<IPublishedContent>>("themeTags");
        foreach(var TagNames in DifferentTags){
            if(@TagNames.Name == @tag.Name && !ShowTag){
                <center><a class="ajaxFilter" data-catId="@tag.Id">@tag.Name</a></center>
                ShowTag = true;
            }
        }
    } 
} 

which displays this:

enter image description here

Can anyone help me with this issue?

EDIT for @DZL

@{ var Artikelen = Model.Content.Children.Where(NieuwsTemplate => NieuwsTemplate.DocumentTypeAlias == "newsDetail"); 
bool ShowTag = false;}

@foreach (var tag in @MainTag.Children){
    foreach (var NewsArticleTags in Artikelen){
        @*var DifferentTags = NewsArticleTags.GetPropertyValue<IEnumerable<IPublishedContent>>("themeTags");*@
        var distinctTags = Artikelen.SelectMany(a => a.GetPropertyValue<IEnumerable<IPublishedContent>>("themeTags")).Select(DifferentTag => DifferentTag.Name).Distinct();
        foreach(var TagNames in distinctTags){
            if(TagNames == @tag.Name)
            {
                <center><a class="ajaxFilter" data-catId="@tag.Id">@tag.Name</a></center>
                ShowTag = true;
            }
        }           
    }        
} 

FINAL CODE THAT FIXED IT

@{
var Artikelen = Model.Content
    .Children
    .Where(c => c.DocumentTypeAlias == "newsDetail");

    var distinctTags = Artikelen
    .SelectMany(a => a.GetPropertyValue<IEnumerable<IPublishedContent>>("themeTags"))
    .GroupBy(node => node.Name)
    .Select(group => group.First());


}

@foreach (var DifferentTags in MainTag.Children){
foreach (var tag in distinctTags)
    {
        if(DifferentTags.Name == tag.Name)
        {
        <center>
            <a class="ajaxFilter" data-catId="@tag.Id">@tag.Name</a>
        </center>
        }
    } 

}

Solution

  • You should use something like this:

    @{
        var Artikelen = Model.Content
            .Children
            .Where(c => c.DocumentTypeAlias == "newsDetail");
    
        var distinctTags = Artikelen
            .SelectMany(a => a.GetPropertyValue<IEnumerable<IPublishedContent>>("themeTags"))
            .GroupBy(node => node.Name)
            .Select(group => group.First());
    }
    
    @foreach (var tag in distinctTags)
    {
        <center>
            <a class="ajaxFilter" data-catId="@tag.Id">@tag.Name</a>
        </center>
    }