recursive

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;


public class Program
{
	public static void Main()
	{
		var relationships = GetRelationships();
		// json
		// desried format
		//		{
		//			"name": "XX Level XX",
		//			"children": [{
		//						"name": "XXX - Level XXX",
		//						"children": [{
		//								"name": "XXXX - Level XXXX",
		//								"children": [{
		//												"name": "XXXXX - Level XXXXX",
		//												"children": [{
		//															"name": "XXXXXX - Level XXXXXX"
		//												}]
		//								}]
		//						}]
		//			}]
		//		}
		
		
		var json = "nope";
		
		dynamic relationship = new JObject();
		relationship.name = relationships[0].Industries[1].Name;
		relationship.children = new JArray();
		
		var lftIndustries = relationships[0].Industries.Where(k => k.Key > 1);
		
		JToken currentContainer = null;
		var lastOne = lftIndustries.Count();
		var idx = 1;
		foreach (var item in lftIndustries)
		{
			var industry = new JObject();
                	// and add "industry" to it
			if (currentContainer != null)
			{
				var node = ((JContainer)currentContainer).Last;
				var childArray = (JArray)((JContainer) node).Last;
	
				if(idx == lastOne)
				{
					industry = new JObject(new JProperty("name", item.Value.Name));	
				}
				else{
				  industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
				}
	
				childArray.Add(industry);
				currentContainer = industry;
			}
			else
			{
				var node = ((JContainer)relationship).Last;
				var childArray = (JArray)((JContainer) node).Last;
	
				if(idx == lastOne)
				{
					industry = new JObject(new JProperty("name", item.Value.Name));	
				}
				else{
					industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
				}
	
				childArray.Add(industry);
				currentContainer = industry;
			}
			idx++;
			    
		}

		json = relationship.ToString();
		
		Console.WriteLine(json);
	}
	
	
	private static List<Relationship> GetRelationships(){
	   var naicsCodes = new List<NaicsCode>();
		naicsCodes.Add(new NaicsCode{Code = "XX", Name = "Level XX", Path = null, PathName = "/3/", Level = 1, IndustryId = "DCA7B69E-0A01-E711-BAE1-D7C01D651811"});
		naicsCodes.Add(new NaicsCode{Code = "XXX", Name = "Level XXX", Path = null, PathName = "/3/17/", Level = 2, IndustryId = "012E0591-3F01-E711-BAE1-D7C01D651811"});
		naicsCodes.Add(new NaicsCode{Code = "XXXX", Name = "Level XXXX", Path = null, PathName = "/3/17/2", Level = 3, IndustryId = "0146576C-5501-E711-BAE1-D7C01D651811"});
		naicsCodes.Add(new NaicsCode{Code = "XXXXX", Name = "Level XXXXX", Path = null, PathName = "/3/17/2/2/2/", Level = 4, IndustryId = "314E09F4-5901-E711-BAE1-D7C01D651811"});
		naicsCodes.Add(new NaicsCode{Code = "XXXXXX", Name = "Level XXXXXX", Path = null, PathName = "/3/17/2/2/1/", Level = 5, IndustryId = "D06C3B31-5B01-E711-BAE1-D7C01D651811"});
		
		var relationships = new List<Relationship>{new Relationship{ClientId = 99, Name = "Apple"}};
		
		//grouping, can have child peers
		var groupedCodes =
			from code in naicsCodes
			group code by code.Level into levelGroup
				orderby levelGroup.Key
				select levelGroup;
		
		foreach (var levelGroup in groupedCodes)
		{
			//once this is figured out loop relationships
			//for now looking at the eaiset json output
			relationships[0].Industries.Add(levelGroup.Key, new Industry());
			foreach (var code in levelGroup)
			{
				var indtry = relationships[0].Industries[levelGroup.Key];
				if (string.IsNullOrEmpty(indtry.Name))
				{
					indtry.Name = string.Format("{0}-{1}", code.Code, code.Name);
				}

				indtry.NaicsCodes.Add(new NaicsCode{Code = code.Code, Name = code.Name, Path = code.Path, PathName = code.PathName, Level = code.Level, IndustryId = code.IndustryId});
			}
		}
		
		
		return relationships;
	
	}
	
		
	public class NaicsCode
    {
		public string Code {get;set;}
		public string Name {get;set;}
		public object Path {get;set;}
		public string PathName {get;set;}
		public int Level {get;set;}
		public string IndustryId {get;set;}
	}

    public class Industry
    {
	   public Industry()
	   {
		NaicsCodes = new List<NaicsCode>();
	   }

	   public string Name {get;set; }
	   public List<NaicsCode> NaicsCodes  {get;set;}
     }

    public class Relationship
    {
	    public Relationship()
	    {
		    Industries = new Dictionary<int, Industry>();
	    }

	    public int ClientId {get;set;}
	    public string Name {get;set;}
	    public Dictionary<int, Industry> Industries{get;set;}
    }
	
}
;