Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Dec 05, 2013 @ 13:58
    Anthony Candaele
    0

    how to check for null value with IPublishedContent

    Hi,

    I'm having a IPublishedContent query:

    var announcement = Model.Content.DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "AnnouncementItem").First();
    

    however this line crashes when there are no announcement items.

    I looked for some documentation here: http://our.umbraco.org/documentation/Reference/Mvc/querying but couldn't found anything.

    Does anyone have an idea how to check for null in this query?

    thanks for your help,

    Anthony

  • Marcio Goularte 388 posts 1360 karma points
    Dec 05, 2013 @ 14:29
    Marcio Goularte
    100

    Hi,

    Use the method FirstOrDefault and check if it is null then.

    http://msdn.microsoft.com/en-us/library/bb340482(v=vs.110).aspx

     

  • Anthony Candaele 1197 posts 2049 karma points
    Dec 05, 2013 @ 14:37
    Anthony Candaele
    0

    Thanks a lot Marcio,

    This worked:

    var announcement = Model.Content.DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "AnnouncementItem").FirstOrDefault();
    

    Greetings,

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft