Copied to clipboard

Flag this post as spam?

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


  • Martin 114 posts 313 karma points
    Apr 14, 2015 @ 21:39
    Martin
    0

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

    I am grateful for any help.

    //martin

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Apr 14, 2015 @ 23:00
    Dennis Aaen
    1

    Hi Martin,

    I think that you can do something like this:

    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.

    Hope this can help you further.

    /Dennis

  • Martin 114 posts 313 karma points
    Apr 15, 2015 @ 21:34
    Martin
    0

    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

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 15, 2015 @ 22:30
    Jan Skovgaard
    0

    Hi Martin

    Just being curious - But would you mind sharing the code you ended up making?

    Cheers, Jan

  • Martin 114 posts 313 karma points
    Apr 16, 2015 @ 19:07
    Martin
    0

    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.

        bool FastBild = true;
    
        try
        {
            @*To check if both fastid AND image exist *@
        var test = homePage.Descendants("FastighetPresentation").Where("FastighetsId = @0", @row.Fastid).First();
        var test2 = test.Bilds.First();
        }
        catch (Exception ex)
        {
            @*ex.Message;*@
            FastBild = false;
        }
    
        if(FastBild == true)
        {
         @*DO SOMETHING *@               
        }
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 16, 2015 @ 21:36
    Dan Diplo
    102

    Why not use:

    int PresId;
    var homePage = Model.Content.AncestorOrSelf(1);
    var item = homePage.Descendants("Presentation").
        FirstOrDefault(p => p.HasValue("PresentationId") && p.GetPropertyValue<int>("PresentationId") == PresId);
    
    if (item != null)
    {
        // do something
    }
    
    By using FirstOrDefault you won't get an exception if it doesn't find anything, you just need to perform a null check.
  • Martin 114 posts 313 karma points
    Apr 17, 2015 @ 21:00
    Martin
    0

    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
     }
    
Please Sign in or register to post replies

Write your reply to:

Draft