Reputation: 425
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>
}
}
}
Upvotes: 1
Views: 290
Reputation: 6050
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>
}
Upvotes: 1
Reputation: 121
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
Upvotes: 0