Copied to clipboard

Flag this post as spam?

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


  • Dan 1285 posts 3917 karma points c-trib
    Feb 18, 2014 @ 19:15
    Dan
    0

    Conditional inside linq 'where' to select nodes where MNTP value equals something

    Hi,

    This sounds more complicated than it is ;) - at least in terms of what I'm trying to do.

    I have a press releases section. Each press release contains a MNTP of 'brands'. So each press release can be associated to one or more brands via the MNTP.

    This razor selection works nicely:

    var brandId = "1234";
    var pressReleaseArticles = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.GetProperty("brand").Value.ToString().Split(',').Contains(brandId));
    

    However, it fails if there are any press releases which don't have a brand selected. I've tried putting a conditional in the statement like this, but it doesn't seem that this type of query is supported:

    var pressReleaseArticles = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.HasProperty("brand") ? x.GetProperty("brand").Value.ToString().Split(',').Contains(brandId) : x.IsVisible);
    

    Can anyone suggest how to do this with Razor. I'd rather not have to stick with the current solution of iterating through all press releases and creating a nodeset of ones matching the brand, to have to iterate through these and output them; I'd prefer to just get the appropriate nodes in the initial query.

    Thanks folks.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 18, 2014 @ 19:42
    Jeavon Leopold
    101

    Hey Dan,

    How about this?

        var pressReleaseArticles = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.HasProperty("brand") && x.HasValue("brand") && x.GetPropertyValue<string>("brand").ContainsAny(brandId));        
    

    Jeavon

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 18, 2014 @ 19:46
    Jeavon Leopold
    0

    Adding the Visible condition

        var pressReleaseArticles = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.HasProperty("brand") && x.HasValue("brand") && x.IsVisible() && x.GetPropertyValue<string>("brand").ContainsAny(brandId));        
    
  • Dan 1285 posts 3917 karma points c-trib
    Feb 19, 2014 @ 11:06
    Dan
    0

    Excellent, thanks Jeavon. Chaining 'HasProperty', 'HasValue' then 'GetProperty' seems to have done the trick. Although I did start out by doing the casting of the property to a string, as per your suggestions, for some reason I only get results returned if I get the value and call the 'ToString' method on it, like this:

    x => x.HasProperty("brand") && x.HasValue("brand") && x.GetProperty("brand").Value.ToString().Split(',').Contains(brandId)
    

    Anyhow, seems to be working nicely, so thanks again :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 19, 2014 @ 11:34
    Jeavon Leopold
    0

    Great! But not sure about why you have the issue with the GetPropertyValue.

    x.GetProperty("brand").Value.ToString()
    

    Should be the same as the recommended

    x.GetPropertyValue<string>("brand")
    

    If it's not there is a problem. What version of Umbraco are you on?

  • Dan 1285 posts 3917 karma points c-trib
    Feb 19, 2014 @ 12:18
    Dan
    0

    Hi Jeavon,

    Yes, it has puzzled me, but I can confirm it is the case. I tried these two options in my view just now (literally character-for-character):

    pressReleases = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.HasProperty("brand") && x.HasValue("brand") && x.GetProperty("brand").Value.ToString().Split(',').Contains(brandId));
    
    pressReleases = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.HasProperty("brand") && x.HasValue("brand") && x.GetPropertyValue<string>("brand").Split(',').Contains(brandId));
    

    The first one returns content, the second returns nothing (doesn't error, just returns nothing).

    I don't know why this is but I'm sure it's something I've come across with other 'similar' datatypes before, namely the uComponents URL picker and Multi-URL picker, and the MNTP as above. Although I don't have an example I can easily test currently, I'm often only able to get what I need out of the properties by doing something like:

    var something = Model.Content.GetProperty("myPropertyAlias").Value;
    

    Rather than:

    var something = Model.Content.GetPropertyValue("myPropertyAlias");
    

    Only for these data-types, the others behave as expected. It may be something to do with using custom models rather than standard Umbraco models, as I didn't build that part of the project I'm working on currently, but I don't know enough about it to know why it's behaving like this. (v6.1.6 by the way.)

    Ultimately it's working, so there's no problem as such, it just makes it a little difficult for a razor noob to grasp what's going on.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 19, 2014 @ 12:49
    Jeavon Leopold
    1

    Hey Dan,

    Only reason I can think of is if you had a Property Editor Value Converter installed for MNTP (like my package), then this would be expected?

    Jeavon

  • Dan 1285 posts 3917 karma points c-trib
    Feb 19, 2014 @ 12:53
    Dan
    0

    Ah, that's highly likely, I'll check. Thanks for your input on this, much appreciated :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 19, 2014 @ 12:59
    Jeavon Leopold
    0

    Ah cool, completely untested but if that is the case, I guess something like this should work:

    var pressReleases = Umbraco.TypedContent(pressReleasesId).Children.Where(x => x.HasProperty("brand") && x.HasValue("brand") && x.GetPropertyValue<IEnumerable<IPublishedContent>>("brand").Any(y => y.Id == brandId));
    
Please Sign in or register to post replies

Write your reply to:

Draft