Home Looping multiple items but only loop unique ones
Reply: 2

Looping multiple items but only loop unique ones

Mik3NL
1#
Mik3NL Published in 2018-01-11 16:16:36Z

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

This is the result of my loop:

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:

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>
        }
    } 

}
DZL
2#
DZL Reply to 2018-01-11 17:19:04Z

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>
} 
Noob dev
3#
Noob dev Reply to 2018-01-11 17:02:25Z

What about saving the last tag ?

@{ var Artikelen = Model.Content.Children.Where(NieuwsTemplate => 
NieuwsTemplate.DocumentTypeAlias == "newsDetail"); 
var lastTag = null;}

@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 && @TagNames.Name != @lastTag.Name)
            {
                <center><a class="ajaxFilter" data-catId="@tag.Id">@tag.Name</a></center>
                @lastTag.Name = @TagNames.Name
            }
        }
    } 
} 

I'm sorry i don't how to use those tag's property but i think you can get what i'm trying to explain

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314894 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO