循环多个项目,但只循环唯一的项目

时间:2018-01-11 16:16:36

标签: c# loops umbraco

我目前有一个循环,循环从Umbraco动态加载的不同标签。

这是我的循环的结果: enter image description here

循环代码:

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

我决定添加一个bool,所以它只循环1个。我显然搞砸了,因为它只循环第一个标签。我想在哪里放bool所以它只循环每个标签一次? 1“游戏化”1“VR / AR”和1“Leren Programmeren”

以下是添加bool的代码:

@{ 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;
            }
        }
    } 
} 

显示:

enter image description here

任何人都可以帮我解决这个问题吗?

编辑@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;
            }
        }           
    }        
} 

固定的最终法典

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

}

2 个答案:

答案 0 :(得分:1)

你应该使用这样的东西:

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

答案 1 :(得分:0)

保存最后一个标签怎么样?

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

对不起,我不知道如何使用这些标签的属性,但我认为你可以得到我试图解释的内容