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.
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 :)
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.
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:
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:
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.
Hey Dan,
How about this?
Jeavon
Adding the Visible condition
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:
Anyhow, seems to be working nicely, so thanks again :)
Great! But not sure about why you have the issue with the GetPropertyValue.
Should be the same as the recommended
If it's not there is a problem. What version of Umbraco are you on?
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):
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:
Rather than:
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.
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
Ah, that's highly likely, I'll check. Thanks for your input on this, much appreciated :)
Ah cool, completely untested but if that is the case, I guess something like this should work:
is working on a reply...