Exemplo n.º 1
0
        // GET: api/Default
        public IEnumerable <string> Get()
        {
            FlatData[] elements = new FlatData[]
            {
                new FlatData {
                    Id = 1, Text = "A"
                },
                new FlatData {
                    Id = 2, Text = "B"
                },
                new FlatData {
                    Id = 3, ParentId = 1, Text = "C"
                },
                new FlatData {
                    Id = 4, ParentId = 1, Text = "D"
                },
                new FlatData {
                    Id = 5, ParentId = 2, Text = "E"
                }
            };

            IEnumerable <NodeData> nodes = elements.RecursiveJoin(element => element.Id,
                                                                  element => element.ParentId,
                                                                  (FlatData element, IEnumerable <NodeData> children) => new NodeData()
            {
                Text     = element.Text,
                Children = children
            });
            var t = nodes.ToList();
            var p = JsonConvert.SerializeObject(t);

            return(new string[] { "value1", "value2" });
        }
        public void RecursiveJoinTest()
        {
            FlatData[] _elements = new FlatData[]
            {
                new FlatData {
                    Id = 1, Text = "A"
                },
                new FlatData {
                    Id = 2, Text = "B"
                },
                new FlatData {
                    Id = 3, ParentId = 1, Text = "C"
                },
                new FlatData {
                    Id = 4, ParentId = 1, Text = "D"
                },
                new FlatData {
                    Id = 5, ParentId = 2, Text = "E"
                }
            };
            IEnumerable <NodeData> _nodes = _elements.RecursiveJoin(element => element.Id,
                                                                    element => element.ParentId,
                                                                    (FlatData element, IEnumerable <NodeData> children) => new NodeData()
            {
                Text     = element.Text,
                Children = children
            });

            Assert.AreEqual(2, _nodes.Count());
            IEnumerable <DeepNodeData> _nodes2 = _elements.RecursiveJoin(element => element.Id, element => element.ParentId, (FlatData element, int index, int depth, IEnumerable <DeepNodeData> children) =>
            {
                return(new DeepNodeData()
                {
                    Text = element.Text,
                    Children = children,
                    Depth = depth
                });
            });

            Assert.AreEqual(2, _nodes2.Count());
        }