I think this reads a bit better, but would guess it might perform slightly worse.
I often use a static helper method something like:
/// <summary>
/// Converts a delimitted string to an enumarable of integers
/// </summary>
/// <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)
{
if (String.IsNullOrEmpty(csvString))
yield break;
var values = csvString.Split(separator);
int d;
foreach (var item in values)
{
if (int.TryParse(item, out d))
{
yield return d;
}
else if (addDefaultOnFail)
{
yield return default(int);
}
}
yield break;
}
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.
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.
How to filter nodes based on content of property whose type is MultiNodeTreePicker
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:
But this kind of ugly and not sure about performances.
Is there a better way of doing this?
Simone
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:
I think this reads a bit better, but would guess it might perform slightly worse.
I often use a static helper method something like:
You can then call it like this:
Again, not sure if it's more readable, but should be efficient and more resilient.
Thx. 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?
the MultiNodeTreePickerPropertyConverter
this is the source or the Core Property Converter package.
Here's my take at it using 7.11.1:
Some explanation:
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:
https://our.umbraco.com/documentation/Reference/Common-Pitfalls/#querying-with-descendants-descendantsorself
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...