Search code examples
c#linqtreeviewilistradtreeview

Convert List of Tree View nodes to combined record


I have no sample code in this instance because i havent a clue where to start and its killing me!

Basically i have a RadTreeView collection of nodes that are checked and these are returned as

IList<RadTreeNode> nodeCollection = RadTreeView1.CheckedNodes;

The format of these values is as follows,

001001009001, 001001009002, 001001009003, 001001009004, 001001009005, 001001010002, 001001010003

This may seem complicated but each set of 3 numbers refers to an element eg 001 001 009 001

I have 5 privileges in total which are reflected by the final three characters, these may be checked or unchecked if they are not in the list i'd like a zero in the output otherwise a one. The other groups refer to locations

Sample output: 001,001,009,1,1,1,1,1 (because in this instance all five privs have been checked) 001,001,010,0,1,1,0,0 (because only 2 & 3 have been checked)

Any help is greatly appreciated!!

Thanks


Solution

  • static void Show()
    {
        IEnumerable<string> lst = new string[]{"001001009001", "001001009002", 
           "001001009003", "001001009004", "001001009005", "001001010002", 
           "001001010003" };
        var res = Process(lst);
        forech(string str in res)
        {
            Console.WriteLine(str);
        }
    }
    static List<string> Process(IEnumerable<string> input)
    {
        Dictionary<string, int[]> elements = new Dictionary<string,int[]>();
    
        foreach (var str in input)
        {
            string element = str.Substring(0, 9);
            int privelegeNumber = int.Parse(str.Substring(9, 3));
            if (!elements.ContainsKey(element))
            {
                elements.Add(element, Enumerable.Repeat(0, 5).ToArray());
            }
            elements[element][privelegeNumber - 1] = 1;
        }
        List<string> result = new List<string>();
        foreach (var element in elements)
        {
            StringBuilder sb = new StringBuilder(21);
            sb.Append(element.Key.Substring(0, 3));
            sb.Append(",");
            sb.Append(element.Key.Substring(3, 3));
            sb.Append(",");
            sb.Append(element.Key.Substring(6, 3));
            foreach (int privelege in element.Value)
            {
                sb.Append(",");
                sb.Append(privelege);
            }
            result.Add(sb.ToString());
        }
        return result;
    }