Copied to clipboard

Flag this post as spam?

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

  • Matt Speakman 11 posts 44 karma points
    Jun 11, 2015 @ 10:18
    Matt Speakman

    Umbraco doing strange things when comparing GetPropertyValue values

    Please take a look at this screen grab:

    Screengrab of a few watches in Visual Studio 2013 Ultimate

    In this screengrab "currentPage" is the Umbraco current page and "post" is an individual page of the same document type as the currentPage. I am attempting to filter the results of a search where the Property ("holidayPark") is similar and is set as another page (A content picker) in Umbraco. Put shortly our Umbraco content is a holiday home and it has a holiday park set on it. The aim is to only display an Umbraco document if "holidayPark" is the same on both pages. As you can see they are the same however

    (post.GetPropertyValue("holidayPark") == currentPage.GetPropertyValue("holidayPark"))

    returns false whereas if we cast to string

    (post.GetPropertyValue<string>("holidayPark") == currentPage.GetPropertyValue<string>("holidayPark")) 

    returns true. The even stranger thing happens when we are sorting the list if the post in the list is the same page (umbraco content) as the currentPage. Then the first block of code returns true however on different pages (umbraco content) it returns false.

    My first guess is the type of encoding in the database is different in some way however I would really like to know why this happens.

    If anyone has any comments then please leave them but I thought I would put my findings for other developers to read and take note.

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 11, 2015 @ 12:20
    Jamie Pollock

    Hi Matt,
    This is a general quirk with .net rather than Umbraco.

    Basically == and obj1.Equals(obj2) are two different things. This stackoverflow post will be able to explain it in more detail. I hope that helps :)

    The best approach is to cast them with GetPropertyValue<string>("alias") so you know esactly what you're getting back.


  • Simon steed 373 posts 685 karma points
    Jun 11, 2015 @ 21:04
    Simon steed

    Yup I agree - the third line will not compute as the objects are differing types hence why it returns false

    Casting them to their respective type is the best way around this, you know what is expected and can compare accordingly like with like


  • Matt Speakman 11 posts 44 karma points
    Jun 12, 2015 @ 11:10
    Matt Speakman

    Thanks for clearing this up for me. I think the answer is clearly to use obj.Equals(obj2) rather than dynamic casting to make sure you are comparing what you want to compare.

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 12, 2015 @ 11:15
    Jamie Pollock

    Yea, I agree in general I tend to use object.Equals() even on strongly typed objects like string with string.Equals() it allows a bit more reliability and functionality like StringComparison to fine tune how I want to compare two objects.

    Be sure to make my answer as the solution please, glad I could help :)

Please Sign in or register to post replies

Write your reply to: