Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I've a list of nodes which contain a property (called "topics") which is a MultiNodeTreePicker.
In a page I've to list all the nodes about a specific topic, so what I should do it search for all nodes that have a specific id in the topics field.
At the moment I'm doing:
x.ContentType.Alias == "Meeting" &&
But this kind of ugly and not sure about performances.
Is there a better way of doing this?
If you install the Core Property converters package - https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/ - then you should be able to do:
.Where(x => x.ContentType.Alias == "Meeting" && x.GetPropertyValue<IEnumerable<IPublishedContent>>("topics")
.Select(p => p.Id).
I think this reads a bit better, but would guess it might perform slightly worse.
I often use a static helper method something like:
/// Converts a delimitted string to an enumarable of integers
/// <param name="csvString">The CSV string</param>
/// <param name="separator">The delimiter character</param>
/// <param name="addDefaultOnFail">Set to true to add the default value for type T if the conversion fails</param>
/// <returns>A list of integers</returns>
public static IEnumerable<int> ToIntCsvList(string csvString, char separator = ',', bool addDefaultOnFail = false)
var values = csvString.Split(separator);
foreach (var item in values)
if (int.TryParse(item, out d))
yield return d;
else if (addDefaultOnFail)
yield return default(int);
You can then call it like this:
.Where(x => x.ContentType.Alias == "Meeting" &&
Again, not sure if it's more readable, but should be efficient and more resilient.
I guess I'll go with the helper as I don't need the actual node but just need to filter them based on Id :)
Hopefully the new Model Builder will handle this
What the Property value converters do is calling a UmbracoHelper.TypedContent(myIEnumerableOfIds). This will use the cache and is pretty quick.
Which Property Value converter?
this is the source or the Core Property Converter package.
Here's my take at it using 7.11.1:
posts = Umbraco.TypedContent(rootNodeId)
.Where(x => x.GetPropertyValue<List<IPublishedContent>>("otherCategories").Any(c => c.Id == categoryNode.Id))
.OrderByDescending(x => x.GetPropertyValue<DateTime>("publishedDate"));
This is working great for me. I hope it helps you.
Just be aware that calling Descendants() of the root node can have horrible performance issues on sites with a lot of nodes, as it has to iterate over every single node.
It's one of the anti-patterns outlined here:
You'll get away with it on smaller sites, especially if you cache results in a partial, but beware. If all your articles are stored under one parent node it's better to start at the parent node and just get the children.
Indeed. You are right.
I am using Descendants() on a very specific task. It should be replaced with Children(). I will update accordingly.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted