Can't find out how to check if certain values of property in doctype exist
Hi,
I need to check if there exists a numeric field property with a certain value for a doctype. Below my doctype is "Presentation" and the property alias is "PresentationId" and the property value to search for is "PresId". That is, if the specified PresId does not have a match, false should be returned.
I would believe that IsNull or HasValue are alternatives to use but I can't figure out how to apply any of them.
var homePage = CurrentPage.AncestorsOrSelf(1).First();
if(homePage.Descendants("Presentation").Where("PresentationId = @0", PresId).First() IS NULL OR HAS VALUE ???)
{
DO SOMETHING;
}
var homePage = CurrentPage.AncestorsOrSelf(1).First(); var item = homePage.Descendants("Presentation").Where("PresentationId = @0", PresId).First();
if(item.HasValue("PresentationId")) { DO SOMETHING; }
Or you could try something like this too.
var homePage = CurrentPage.AncestorsOrSelf(1).First();
if(!string.IsNullOrWhiteSpace(homePage.Descendants("Presentation").Where("PresentationId = @0", PresId).First())) { DO SOMETHING; }
Try to see these Razor cheatsheets https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets, I know it says for Umbraco 6, but you can also use it for Umbraco 7 it´s an pdf file with the different methods in. As you can see you have HasValue to check if the fields contains any value, and you have HasProperty to check if the property is on the document type.
There are a cheatsheet for the dynamic version of Razor and one for the strongly typed version.
Thanks for the hint Dan. FirstOrDefault I did not know of until now.
I first got an compiler error:
Compiler Error Message: CS1977: Cannot use a lambda expression as an
argument to a dynamically dispatched operation without first casting
it to a delegate or expression tree type
So I needed to change the code a little bit, but now it works fine without having a try catch block:
(Note, I have also renamed the properties name)
var homePage = CurrentPage.AncestorsOrSelf(1).First(); //homePage = Model.Content.AncestorOrSelf(1) did not work
var fastighet = homePage.Descendants("FastighetPresentation").Where("FastighetsId = @0", Fastid).FirstOrDefault();
var bilder = fastighet.Bilds.FirstOrDefault();
if (bilder != null)
{
// do something
}
Can't find out how to check if certain values of property in doctype exist
Hi,
I need to check if there exists a numeric field property with a certain value for a doctype. Below my doctype is "Presentation" and the property alias is "PresentationId" and the property value to search for is "PresId". That is, if the specified PresId does not have a match, false should be returned.
I would believe that IsNull or HasValue are alternatives to use but I can't figure out how to apply any of them.
I am grateful for any help.
//martin
Hi Martin,
I think that you can do something like this:
Or you could try something like this too.
Try to see these Razor cheatsheets https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets, I know it says for Umbraco 6, but you can also use it for Umbraco 7 it´s an pdf file with the different methods in. As you can see you have HasValue to check if the fields contains any value, and you have HasProperty to check if the property is on the document type.
There are a cheatsheet for the dynamic version of Razor and one for the strongly typed version.
Hope this can help you further.
/Dennis
Thanks Dennis,
I didn't get it to work, but I tested a try catch block instead, which I got to work. So a little bit of work-around but it running.
// Martin
Hi Martin
Just being curious - But would you mind sharing the code you ended up making?
Cheers, Jan
Absolutely Jan, see below.
I use a boolean variable which is set to false if an exception is thrown. It works fine for my purpose as of now.
Why not use:
Thanks for the hint Dan.
FirstOrDefault
I did not know of until now.I first got an compiler error:
So I needed to change the code a little bit, but now it works fine without having a try catch block:
(Note, I have also renamed the properties name)
is working on a reply...