How can I remove duplicate values derived from a Multinode Treepicker?
Using Umbraco version 7.2.8
I'm attempting to categorize "article" sub-sub pages for a certain template while minimizing the amount of effort for users (i.e., I didn't want anyone to have to repeatedly type out each category for each article). I figured that a Multinode Treepicker would be the easiest option for users.
I also want to be able to organize the visible list of articles by category into a drop-down menu on the Landing Page. So far, I've been able to get the categories to list properly with this code:
@{
var listHold = CurrentPage.AncestorOrSelf(1).Descendants("ArticleItem");
foreach (var articleItem in listHold)
{
var categoriesList = articleItem.itemCategories.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var catCollection = Umbraco.Content(categoriesList);
foreach (var cat in catCollection)
{
<p>@cat.Name</p>
}
}
}
But the categories repeat for each article. I only want the category to show up once in the list, no matter how many articles have the same category.
I still have duplicates showing in my list. I have tried virtually every permutation of "Distinct" -- at virtually every possible location within my code -- I could find through searching for this answer, but none of them seems to work.
If changing the Descendents or Dynamics usage might fix this, please let me know. Right now, I'm only concerned with making the correct content appear and this method is the only way I've found that does that.
I think the Distinct call would work if you just had one list to get ids from but - I think you need to hold the list of ids in a separate list while you loop through each article and then remove duplicates before you show them.
var listHold = Umbraco.AssignedContentItem.AncestorOrSelf(1).Descendants("ArticleItem");
var allCategories = new List<string>();
foreach (var articleItem in listHold)
{
var categoriesList = articleItem.GetPropertyValue<string>
("itemCategories").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Distinct();
allCategories.AddRange(categoriesList);
}
var catCollection = Umbraco.TypedContent(allCategories.Distinct());
foreach (var cat in catCollection)
{
<p>@cat.Name</p>
}
this is two loops the first gets the unique ids across all the properties, and the second displays them. you could probably do something clever in the AddRange() call to only add the ones that aren't already in the list, but for simplicity, I've left it adding duplicates and you removing them on the second loop.
How can I remove duplicate values derived from a Multinode Treepicker?
Using Umbraco version 7.2.8
I'm attempting to categorize "article" sub-sub pages for a certain template while minimizing the amount of effort for users (i.e., I didn't want anyone to have to repeatedly type out each category for each article). I figured that a Multinode Treepicker would be the easiest option for users.
I also want to be able to organize the visible list of articles by category into a drop-down menu on the Landing Page. So far, I've been able to get the categories to list properly with this code:
But the categories repeat for each article. I only want the category to show up once in the list, no matter how many articles have the same category.
Hi Marie
First of all, do not use dynamics and do not use "Descendants", please read this article - https://our.umbraco.org/documentation/reference/Common-Pitfalls/
I would remove duplicates like that:
Distinct() method should help
Thanks,
Alex
Hi Alex,
I still have duplicates showing in my list. I have tried virtually every permutation of "Distinct" -- at virtually every possible location within my code -- I could find through searching for this answer, but none of them seems to work.
If changing the Descendents or Dynamics usage might fix this, please let me know. Right now, I'm only concerned with making the correct content appear and this method is the only way I've found that does that.
Hi
I think the Distinct call would work if you just had one list to get ids from but - I think you need to hold the list of ids in a separate list while you loop through each article and then remove duplicates before you show them.
this is two loops the first gets the unique ids across all the properties, and the second displays them. you could probably do something clever in the AddRange() call to only add the ones that aren't already in the list, but for simplicity, I've left it adding duplicates and you removing them on the second loop.
That works! Thank you, Kevin!
Apologies for dragging this very old cow from the moat but I have similar issue for removing duplicates from my tags.
In case someone can shed a light. This is my topic. https://our.umbraco.com/forum/templates-partial-views-and-macros/93507-umbraco-tags-filter-out-multiple-tags-that-lead-to-the-same-page
Thanks in advance!
The thread vanished?
https://our.umbraco.com/forum/templates-partial-views-and-macros/93507-umbraco-tags-filter-out-multiple-tags-that-lead-to-the-same-page
is working on a reply...