Umbraco doing strange things when comparing GetPropertyValue values
Please take a look at this screen grab:
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
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.
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.
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 :)
Umbraco doing strange things when comparing GetPropertyValue values
Please take a look at this screen grab:
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
returns false whereas if we cast to string
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.
Hi Matt,
This is a general quirk with .net rather than Umbraco.
Basically
==
andobj1.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.Thanks,
Jamie
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
Simon
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.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 :)
is working on a reply...