/// <summary> /// Rebuild the list in the correct order when a child collection /// changes. /// </summary> private void Rebuild() { this.Mutate(that => { IList <T> items = ChildCollections.SelectMany(collection => collection).ToList(); foreach (T item in items) { that.Remove(item); } foreach (T item in items) { that.Add(item); } }); }
private static IEnumerable <JToken> GetFlattenedObjects(JToken token, IEnumerable <JProperty> otherProperties = null) { if (token is JObject obj) { var children = obj.Children <JProperty>().GroupBy(prop => prop.Value?.Type == JTokenType.Array).ToDictionary(gr => gr.Key); if (children.TryGetValue(false, out var directProps)) { otherProperties = otherProperties?.Concat(directProps) ?? directProps; } if (children.TryGetValue(true, out var ChildCollections)) { foreach (var childObj in ChildCollections.SelectMany(childColl => childColl.Values()).SelectMany(childColl => GetFlattenedObjects(childColl, otherProperties))) { yield return(childObj); } } else { var res = new JObject(); if (otherProperties != null) { foreach (var prop in otherProperties) { res.Add(prop); } } yield return(res); } } else if (token is JArray arr) { foreach (var co in token.Children().SelectMany(c => GetFlattenedObjects(c, otherProperties))) { yield return(co); } } else { throw new NotImplementedException(token.GetType().Name); } }