Sitecore
Tree Menu Traversal in C# Queryable
Using C# to query entire tree system of pages looking for a particular attribute. Normally I would use a recursive technique, but then found this to be easier and more readable. Not sure how it is on performance, I think it’s supposed to be better. Found it here http://stackoverflow.com/questions/7062882/searching-a-tree-using-linq#answer-7063002 Here’s how I implmented
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class MenuItem { public virtual string NavigationTitle { get; set; } public virtual string Url { get; set; } public virtual bool ShowInMainNavigation { get; set; } public virtual bool ShowInFooterNavigation { get; set; } public virtual IEnumerable Children { get; set; } public virtual string TwitterHandle { get; set; } public virtual string FacebookHandle { get; set; } public virtual string InstagramHandle { get; set; } public IEnumerable GetDescendants(MenuItem root) { var nodes = new Stack(new[] { root }); while (nodes.Any()) { MenuItem node = nodes.Pop(); yield return node; foreach (var n in node.Children) nodes.Push(n); } } } |
Here’s […]