public JsonObjectList GroupBy(string[] groupFields, string[] groupNames) { var keyfields = groupFields[0].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var res = new JsonObjectList(); var groups = new Dictionary <string, List <JsonObject> >(); foreach (var elt in _items) { var keys = new string[keyfields.Length]; for (var i = 0; i < keyfields.Length; i++) { var val = elt[keyfields[i]]; keys[i] = val != null?val.ToString() : "NULL"; } var k = string.Join("#", keys); if (!groups.ContainsKey(k)) { groups.Add(k, new List <JsonObject>()); } groups[k].Add(elt); } foreach (var k in groups.Keys) { var o = new JsonObject(); var g = groups[k]; foreach (var t in keyfields) { o.AddProperty(t, g[0][t]); } var items = new JsonObjectList { g.ToArray() }; if (groupFields.Length > 1) { var nextFields = new string[groupFields.Length - 1]; Array.Copy(groupFields, 1, nextFields, 0, groupFields.Length - 1); var nextGroups = new string[groupNames.Length - 1]; Array.Copy(groupNames, 1, nextGroups, 0, groupNames.Length - 1); items = items.GroupBy(nextFields, nextGroups); } o.AddProperty(groupNames[0], items); res.Add(o); } return(res); }
public JsonObjectList FilterBy(string field, object value) { var result = new JsonObjectList(); foreach (var jso in _items) { if (jso[field].Equals(value)) { result.Add(jso); } } return(result); }
/// <summary> /// Join the JsonObjects from data to the JsonObject of target. /// </summary> /// <param name="target">The list of JsonObject to put the new object into</param> /// <param name="data">The data to join to the target</param> /// <param name="pattern">A pattern corresponding to the path within the data to find the items to be joined with the target</param> /// <param name="propertyName">The property to add to regroup all the data joined, /// set this parameter to null to merge the data with the target.</param> /// <param name="localField">The name of the foreign key if not equals to propertyname</param> public static void Join(List <JsonObject> target, JsonObjectList data, string pattern, string propertyName, string localField) { var i = pattern.LastIndexOf("."); var field = i > 0 ? pattern.Substring(i + 1) : pattern; var path = i > 0 ? pattern.Substring(0, i) : ""; var groups = new Dictionary <string, List <JsonObject> >(); foreach (var elt in data.FindChildren(path)) { if (elt == null) { continue; } var k = elt[field].ToString(); if (!groups.ContainsKey(k)) { groups.Add(k, new List <JsonObject>()); } groups[k].Add(elt); } foreach (var jo in target) { var k = jo[localField].ToString(); if (groups.ContainsKey(k)) { if (propertyName != null) { var items = new JsonObjectList { groups[k] }; jo.AddProperty(propertyName, items); } else { if (groups[k].Count > 0) { jo.AddObject(groups[k][0]); } } } } }
/// <summary> /// Join the data (1 - 1) to the current JsonObjectList /// </summary> /// <param name="data">The data to join with</param> /// <param name="field">The relation field</param> public void Join(JsonObjectList data, string field) { Join(data, field, null, field); }
/// <summary> /// Join the data (1 - n) to the current JsonObjectList /// </summary> /// <param name="data">The data to join with</param> /// <param name="field">The relation field</param> /// <param name="propertyName">The property to create to regroup all the children from data. Null for a (1 - 1) relation</param> public void Join(JsonObjectList data, string field, string propertyName) { Join(data, field, propertyName, field); }
/// <summary> /// Join the data (1 - n) to the current JsonObjectList /// </summary> /// <param name="data">The data to join with</param> /// <param name="pattern">A pattern corresponding to the path within the data to find the items to be joined with the target</param> /// <param name="propertyName">The property to create to regroup all the children from data. Null for a (1 - 1) relation</param> /// <param name="localField">The field in the current object to be used as a foreign key</param> public void Join(JsonObjectList data, string pattern, string propertyName, string localField) { Join(_items, data, pattern, propertyName, localField); }