Exemplo n.º 1
0
        public Dictionary <string, Region> SetupTree(MyContext context, Regions regionsTree)
        {
            void Visitor(List <EntityTree <Region> > items, Region parent, Action <EntityTree <Region>, Region> visitAction)
            {
                if (items == null)
                {
                    return;
                }

                foreach (var entityTree in items)
                {
                    visitAction(entityTree, parent);
                    Visitor(entityTree.Children, entityTree.Item, visitAction);
                }
            }

            List <Region> regionsFlat = new List <Region>();

            Visitor(regionsTree.Cast <EntityTree <Region> >().ToList(), null, (tree, parent) => context.Regions.AddNextChild(parent, tree.Item));
            Visitor(regionsTree.Cast <EntityTree <Region> >().ToList(), null, (tree, region) => regionsFlat.Add(tree.Item));

            context.SaveChanges();

            return(regionsFlat.ToDictionary(r => r.Name, r => r));
        }