/// <summary>Returns a child node of the parent.</summary> /// <param name="child1"> null or the hierarchyid of a child of the current node. </param> /// <param name="child2"> null or the hierarchyid of a child of the current node. </param> /// <returns> /// Returns one child node that is a descendant of the parent. /// If parent is null, returns null. /// If parent is not null, and both child1 and child2 are null, returns a child of parent. /// If parent and child1 are not null, and child2 is null, returns a child of parent greater than child1. /// If parent and child2 are not null and child1 is null, returns a child of parent less than child2. /// If parent, child1, and child2 are not null, returns a child of parent greater than child1 and less than child2. /// If child1 is not null and not a child of parent, an exception is raised. /// If child2 is not null and not a child of parent, an exception is raised. /// If child1 >= child2, an exception is raised. /// </returns> public HierarchyId GetDescendant(HierarchyId child1, HierarchyId child2) { if (this._nodes == null) { return(new HierarchyId((string)null)); } if (child1 != (HierarchyId)null && ((int)child1.GetLevel() != (int)this.GetLevel() + 1 || !child1.IsDescendantOf(this))) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetDescendant failed because '{0}' must be a child of 'this'. '{0}' was '{1}' and 'this' was '{2}'.", (object)nameof(child1), (object)child1, (object)this.ToString()), nameof(child1)); } if (child2 != (HierarchyId)null && ((int)child2.GetLevel() != (int)this.GetLevel() + 1 || !child2.IsDescendantOf(this))) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetDescendant failed because '{0}' must be a child of 'this'. '{0}' was '{1}' and 'this' was '{2}'.", (object)nameof(child2), (object)child1, (object)this.ToString()), nameof(child2)); } if (child1 == (HierarchyId)null && child2 == (HierarchyId)null) { return(new HierarchyId(this.ToString() + (object)1 + "/")); } if (child1 == (HierarchyId)null) { HierarchyId hierarchyId = new HierarchyId(child2.ToString()); int[] numArray = ((IEnumerable <int[]>)hierarchyId._nodes).Last <int[]>(); --numArray[numArray.Length - 1]; return(new HierarchyId("/" + string.Join("/", ((IEnumerable <int[]>)hierarchyId._nodes).Select <int[], string>(new Func <int[], string>(HierarchyId.IntArrayToStirng))) + "/")); } if (child2 == (HierarchyId)null) { HierarchyId hierarchyId = new HierarchyId(child1.ToString()); int[] numArray = ((IEnumerable <int[]>)hierarchyId._nodes).Last <int[]>(); ++numArray[numArray.Length - 1]; return(new HierarchyId("/" + string.Join("/", ((IEnumerable <int[]>)hierarchyId._nodes).Select <int[], string>(new Func <int[], string>(HierarchyId.IntArrayToStirng))) + "/")); } int[] array1 = ((IEnumerable <int[]>)child1._nodes).Last <int[]>(); int[] array2 = ((IEnumerable <int[]>)child2._nodes).Last <int[]>(); if (HierarchyId.CompareIntArrays(array1, array2) >= 0) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetDescendant failed because 'child1' must be less than 'child2'. 'child1' was '{0}' and 'child2' was '{1}'.", (object)child1, (object)child2), nameof(child1)); } int index = 0; while (index < array1.Length && array1[index] >= array2[index]) { ++index; } int[] array = ((IEnumerable <int>)array1).Take <int>(index + 1).ToArray <int>(); if (array[index] + 1 < array2[index]) { ++array[index]; } else { array = ((IEnumerable <int>)array).Concat <int>((IEnumerable <int>) new int[1] { 1 }).ToArray <int>(); } return(new HierarchyId("/" + string.Join("/", ((IEnumerable <int[]>) this._nodes).Select <int[], string>(new Func <int[], string>(HierarchyId.IntArrayToStirng))) + "/" + HierarchyId.IntArrayToStirng((IEnumerable <int>)array) + "/")); }
/// <summary>Returns true if this is a descendant of parent.</summary> /// <returns>True if this is a descendant of parent.</returns> /// <param name="parent">parent</param> public bool IsDescendantOf(HierarchyId parent) { if (parent == (HierarchyId)null) { return(true); } if (this._nodes == null || (int)parent.GetLevel() > (int)this.GetLevel()) { return(false); } for (int index = 0; index < (int)parent.GetLevel(); ++index) { if (HierarchyId.CompareIntArrays(this._nodes[index], parent._nodes[index]) != 0) { return(false); } } return(true); }
public HierarchyId GetReparentedValue(HierarchyId oldRoot, HierarchyId newRoot) { if (oldRoot == (HierarchyId)null || newRoot == (HierarchyId)null) { return(new HierarchyId((string)null)); } if (!this.IsDescendantOf(oldRoot)) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetReparentedValue failed because 'oldRoot' was not an ancestor node of 'this'. 'oldRoot' was '{0}', and 'this' was '{1}'.", (object)oldRoot, (object)this.ToString()), nameof(oldRoot)); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("/"); foreach (int[] node in newRoot._nodes) { stringBuilder.Append(HierarchyId.IntArrayToStirng((IEnumerable <int>)node)); stringBuilder.Append("/"); } foreach (int[] numArray in ((IEnumerable <int[]>) this._nodes).Skip <int[]>((int)oldRoot.GetLevel())) { stringBuilder.Append(HierarchyId.IntArrayToStirng((IEnumerable <int>)numArray)); stringBuilder.Append("/"); } return(new HierarchyId(stringBuilder.ToString())); }