I'm developing a website in umbraco 7 and this is my first Umbraco project so I am still getting to grips with things.
My problem is that my list, lists all tags duplicating, can someone tell me how to remove duplicates from the list, i have tried .Distinct("propertyAlias") but nothing helps.
Are you trying to get a distinct list of tags from within a single content property or a distinct list of tags used by every property within your Umbraco content tree?
It's just for a single property, and i have tried, with @Model.Content.GetPropertyValue("propertyAlias").ToString().Split(',').Distinct(); but it dosent help
The thing i want is to get all the distinct list of tags from within a single content property in a template. here is what i have done so far.
'System.Array' does not contain a definition for 'Distinct'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Distinct'
The return type of Split() should be a string array but your error mentions string. Did you put the ToList() method call after the Split() method call? Your error looks like you may have put it after the ToString() method call.
Also, does your Umbraco site work correctly? For example, you can get to the back office and edit content? I just want to double check your installation works correctly.
@{ var allLectures = Model.AncestorOrSelf(1).DescendantsOrSelf("Foredrag");
foreach(var lecture in allLectures) { var tags = lecture.tags.ToString().Split(',').ToList().Distinct();
foreach (var tag in tags) { @tag } } }
Same type error :/
'string' does not contain a definition for 'ToList'
Can you find anything that stands out?
Otherwise my installation have been quite normal so far, everything working as it should, I can edit content, create macros, partial views and link document types and all that together.. but for some reason this just wont work..
I think I've finally cracked it. I set it up locally, recreated the error and now the following seems to work.
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var allLectures = Model.AncestorOrSelf(1).DescendantsOrSelf("Foredrag");
foreach(var lecture in allLectures)
{
var tags = lecture.tags.Split(',');
var tagsList = new List<string>(tags);
foreach (var tag in tagsList.Distinct())
{
@tag
}
}
}
For example, "Tag" and "Tag " would be treated as non distinct strings. You might have to trim and lowercase all your tags before getting the distinct list.
If it is a distinct list of tags for all lectures, then change your script to the following. Notice how it loops through all lectures, adding to the same list and then finds a distinct list.
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var allLectures = Model.AncestorOrSelf(1).DescendantsOrSelf("Foredrag");
var tagsList = new List<string>();
foreach (var lecture in allLectures)
{
tagsList.AddRange(lecture.tags.Split(','));
}
foreach (var tag in tagsList.Distinct())
{
@tag
}
}
I want to display a distinct list of tags for alle lectures in one page, so i can se the tag list of all the lectures. And i also want to display tags for each lecture in their own lecture page.
I'm not sure ifimexplain myselfwellenough.. do u understand what i mean ?
Rather than outputting a distinct list of tags for each lecture, the code should have retrieved a distinct list of tags of all lectures. So two loops through were added. One to find all tags for all lectures and then a second to loop through each distinct tag.
how to remove duplicates from a tag property
Hi all,
I'm developing a website in umbraco 7 and this is my first Umbraco project so I am still getting to grips with things.
My problem is that my list, lists all tags duplicating, can someone tell me how to remove duplicates from the list, i have tried .Distinct("propertyAlias") but nothing helps.
any ide?
Hi,
Are you trying to get a distinct list of tags from within a single content property or a distinct list of tags used by every property within your Umbraco content tree?
Thanks, Dan.
If it's just for a single property, you could do something like:
If you wanted to find all tags and make sure they are all unique, you could use the Tag Service:
Thanks, Dan.
Hi Dan, thanks for your reply.
It's just for a single property, and i have tried, with @Model.Content.GetPropertyValue("propertyAlias").ToString().Split(',').Distinct(); but it dosent help
The thing i want is to get all the distinct list of tags from within a single content property in a template. here is what i have done so far.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = "Root.cshtml";
var root = Umbraco.Content(1066);
var allLectures = root.DescendantsOrSelf("Foredrag");
foreach( var lecture in allLectures)
{
@lecture.tags
}
}
The output looks like this so far:
Democracy, EU, Trade, Democracy, EU, Trade
What i want too look like is:
Democracy,EU, Trade
Give the following a try. The Distinct() method along with looping through each tag should work.
Just as a side note, you have two different spellings of Demokarti and Demokrati within your example list.
Thanks, Dan.
I'm getting this error:
'System.Array' does not contain a definition for 'Distinct'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Distinct'
Try adding the System.Linq namespace to the top of your view. Something like the following:
Thanks, Dan.
It doesn't help, i'm getting same error.
Hmmm strange. Lets try converting the split string array to a list...
Thanks, Dan.
Same type error :/
'string' does not contain a definition for 'ToList'
It's like it doesnt recognize the using System.Linq at all :(
The return type of Split() should be a string array but your error mentions string. Did you put the ToList() method call after the Split() method call? Your error looks like you may have put it after the ToString() method call.
Also, does your Umbraco site work correctly? For example, you can get to the back office and edit content? I just want to double check your installation works correctly.
Thanks, Dan.
This is my code so far:
@using System.Linq
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var allLectures = Model.AncestorOrSelf(1).DescendantsOrSelf("Foredrag");
foreach(var lecture in allLectures)
{
var tags = lecture.tags.ToString().Split(',').ToList().Distinct();
foreach (var tag in tags)
{
@tag
}
}
}
Same type error :/
'string' does not contain a definition for 'ToList'
Can you find anything that stands out?
Otherwise my installation have been quite normal so far, everything working as it should, I can edit content, create macros, partial views and link document types and all that together.. but for some reason this just wont work..
Any insights?
I'm just guessing now so try using a non-dynamic version of the current page like as follows:
Thanks, Dan.
it dosent work :(
I think I've finally cracked it. I set it up locally, recreated the error and now the following seems to work.
Thanks, Dan.
Sorry for the late replay, but still my code is not working :( dont know what to do
What errors do you get if any? I tried the above a fresh install with what I assume was the same setup and it worked without error.
Thanks, Dan.
Uhmmm strange, now it wokrs im not getting any error, but im still getting duplicate
Have you got similar tags with different casing or spaces at the end?
Thanks, Dan.
at the end of ?? im not sure if im understand your quistion.
For example, "Tag" and "Tag " would be treated as non distinct strings. You might have to trim and lowercase all your tags before getting the distinct list.
Thanks, Dan.
that is not helping either, there is still duplicate
Can you post your full code if possible please?
Thanks, Dan.
i have this in my lecture template:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = "Root.cshtml";
}
<h1>@Umbraco.Field("titelOfLecturer")</h1>
<div style="float:left;padding-right:30px;width:400px;">
<img src="@Umbraco.Field("lecturePicture")">
</div>
<div style="float:left;width:480px;">
@Umbraco.Field("lectureUnderTitel")
@Umbraco.Field("date")
@Umbraco.Field("summary")
@Umbraco.Field("summaryOfLecture")
@Umbraco.Field("auther")
@if (CurrentPage.HasProperty("tags"))
{
var tagsList = CurreantPage.tags;
if(tagsList != null) {
foreach( var tag in tagsList ) {
@tag
}
}
}
</div>
and this in my scripting file:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var allLectures = Model.AncestorOrSelf(1).DescendantsOrSelf("Foredrag");
foreach(var lecture in allLectures)
{
var tags = lecture.tags.Split(',');
var tagsList = new List<string>(tags);
foreach (var tag in tagsList.Distinct())
{
@tag
}
}
}
Thanks. Are you wanting to display a distinct list of tags for all lectures or display a distinct list for each lecture?
Thanks, Dan.
If it is a distinct list of tags for all lectures, then change your script to the following. Notice how it loops through all lectures, adding to the same list and then finds a distinct list.
I want to display a distinct list of tags for alle lectures in one page, so i can se the tag list of all the lectures. And i also want to display tags for each lecture in their own lecture page.
I'm not sure if im explain myself well enough.. do u understand what i mean ?
Okay great. Try the code example above and that should give you a distinct list of all tags for all lectures.
Thanks, Dan.
Thank you very much, now the code is working very well
What was the issue??
Rather than outputting a distinct list of tags for each lecture, the code should have retrieved a distinct list of tags of all lectures. So two loops through were added. One to find all tags for all lectures and then a second to loop through each distinct tag.
Thanks, Dan.
Thank you very much for your time.
is working on a reply...