Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • princess 19 posts 39 karma points
    Sep 10, 2014 @ 15:12
    princess
    0

    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?

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 10, 2014 @ 15:53
    Dan Lister
    0

    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.

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 10, 2014 @ 15:57
    Dan Lister
    1

    If it's just for a single property, you could do something like:

    @Model.Content.GetPropertyValue("propertyAlias").ToString().Split(',').Distinct();
    

    If you wanted to find all tags and make sure they are all unique, you could use the Tag Service:

    TagService.GetAllTags().Distinct();
    

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 11, 2014 @ 09:55
    princess
    0

    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

     

     

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 11, 2014 @ 10:06
    Dan Lister
    0

    Give the following a try. The Distinct() method along with looping through each tag should work.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Root.cshtml";
    
        var root = Umbraco.Content(1066);
        var allLectures = root.DescendantsOrSelf("Foredrag");
    
        foreach(var lecture in allLectures) 
        {
            var tags = lecture.tags.ToString().Split(',').Distinct();
            foreach (var tag in tags)
            {
                @tag
            }
        }
    }
    

    Just as a side note, you have two different spellings of Demokarti and Demokrati within your example list.

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 11, 2014 @ 10:13
    princess
    0

    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'


  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 11, 2014 @ 10:17
    Dan Lister
    0

    Try adding the System.Linq namespace to the top of your view. Something like the following:

    @using System.Linq
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Root.cshtml";
    
        var root = Umbraco.Content(1066);
        var allLectures = root.DescendantsOrSelf("Foredrag");
    
        foreach(var lecture in allLectures) 
        {
            var tags = lecture.tags.ToString().Split(',').Distinct();
            foreach (var tag in tags)
            {
                @tag
            }
        }
    }
    

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 11, 2014 @ 10:20
    princess
    0

    It doesn't help, i'm getting same error.

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 11, 2014 @ 10:29
    Dan Lister
    0

    Hmmm strange. Lets try converting the split string array to a list...

    @using System.Linq
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Root.cshtml";
    
        var root = Umbraco.Content(1066);
        var allLectures = root.DescendantsOrSelf("Foredrag");
    
        foreach(var lecture in allLectures) 
        {
            var tags = lecture.tags.ToString().Split(',').ToList().Distinct();
            foreach (var tag in tags)
            {
                @tag
            }
        }
    }
    

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 11, 2014 @ 10:42
    princess
    0

     

    Same type error :/

    'string' does not contain a definition for 'ToList'

     

    It's like it doesnt recognize the using System.Linq at all :(

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 11, 2014 @ 10:47
    Dan Lister
    0

    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.

  • princess 19 posts 39 karma points
    Sep 11, 2014 @ 14:08
    princess
    0

    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?

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 11, 2014 @ 14:16
    Dan Lister
    0

    I'm just guessing now so try using a non-dynamic version of the current page like as follows:

    @using System.Linq
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
        var allLectures = Current.AncestorOrSelf(1).DescendantsOrSelf("Foredrag");
    
        foreach(var lecture in allLectures) 
        {
            var tags = lecture.GetPropertyValue("tags").Split(',').Distinct();
    
            foreach (var tag in tags)
            {
                @tag
            }
        }
    }
    

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 11, 2014 @ 14:28
    princess
    0

    it dosent work :(

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 11, 2014 @ 15:23
    Dan Lister
    0

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

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 10:35
    princess
    0

    Sorry for the late replay, but still my code is not working :( dont know what to do

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 10:36
    Dan Lister
    0

    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.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 11:22
    princess
    0

    Uhmmm strange, now it wokrs im not getting any error, but im still getting duplicate

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 11:30
    Dan Lister
    0

    Have you got similar tags with different casing or spaces at the end?

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 11:59
    princess
    0

    at the end of ?? im not sure if im understand your quistion.

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 12:01
    Dan Lister
    0

    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.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 12:14
    princess
    0

    that is not helping either, there is still duplicate

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 12:15
    Dan Lister
    0

    Can you post your full code if possible please?

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 13:05
    princess
    0
  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 13:08
    princess
    0

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

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 13:10
    Dan Lister
    0

    Thanks. Are you wanting to display a distinct list of tags for all lectures or display a distinct list for each lecture?

    Thanks, Dan.

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 13:17
    Dan Lister
    0

    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
        }
    }
    
  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 13:19
    princess
    0

    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 ?

     

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 13:20
    Dan Lister
    0

    Okay great. Try the code example above and that should give you a distinct list of all tags for all lectures.

    Thanks, Dan.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 13:23
    princess
    0

    Thank you very much, now the code is working very well

     

     

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 13:26
    princess
    0

    What was the issue??

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 23, 2014 @ 13:29
    Dan Lister
    0

    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.

  • princess 19 posts 39 karma points
    Sep 23, 2014 @ 13:40
    princess
    0

    Thank you very much for your time.

Please Sign in or register to post replies

Write your reply to:

Draft