从文件夹位置列表创建对象层次结构

发布于 2024-11-18 04:25:08 字数 506 浏览 2 评论 0原文

我有一个字符串形式的位置列表;

locA/locB
locA/locB/locH
locC/locD/locE
locC/locD/locE/locK
locF/locG

我一直在尝试创建一个对象,该对象使用与传递给它的位置列表相同的结构;

例如,类似......

var myHObject=CreateHeirarchicalObjectFromList(myStringListOfLocations);

我在循环遍历列表时遇到问题,而几乎没有手动执行大量循环。有没有更简单的方法,也许是递归?

我想最终得到一个像这样的物体;

.locA
    .locB
         .locH
.locC
    .locD
         .locE
              .locK
.locF
     .locG

我可以用它来创建视觉层次结构。

I have a list of locations as strings;

locA/locB
locA/locB/locH
locC/locD/locE
locC/locD/locE/locK
locF/locG

I've been trying to create an object that uses the same structure as the list of locations passed to it;

e.g. Something like..

var myHObject=CreateHeirarchicalObjectFromList(myStringListOfLocations);

I'm having problems looping through the list without almost doing it manually with loads of loops. Is there an easier way, maybe recursion?

I want to end up with an object like this;

.locA
    .locB
         .locH
.locC
    .locD
         .locE
              .locK
.locF
     .locG

That I can use to create a visual hierarchy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-11-25 04:25:08

可能不是最好的,但在 LinqPad 中被撞坏了,将在一秒钟内重新格式化..

    void Main()
    {
        var strings = new string[]{"locA/locB","locA/locB/locH",
                         "locC/locD/locE","locC/locD/locE/locK","locF/locG"};

        var folders = Folder.Parse(strings);

        folders.Dump();
    }


    public class Folder
    {
        public string Name { get; set; }

        public List<Folder> Folders { get; internal set; }

        public Folder()
        {
            Folders = new List<Folder>();
        }
        //Presume that each string will be folder1/folder2/folder3
        public static IEnumerable<Folder> Parse(IEnumerable<string> locations)
        {
            var folders = new List<Folder>();
            foreach (string location in locations)
            {
                string[] parts = location.Split('/');
                Folder current = null;
                foreach (string part in parts)
                {
                    var useFolders = current != null ? 
                               current.Folders : folders;
                    current = useFolders.SingleOrDefault(f => f.Name == part) ?? new Folder() { Name = part };
                    if (!useFolders.Contains(current)) { useFolders.Add(current); }
                }
            }
            return folders;
        }
    }

Prob not the best but knocked up in LinqPad, will reformat in a sec..

    void Main()
    {
        var strings = new string[]{"locA/locB","locA/locB/locH",
                         "locC/locD/locE","locC/locD/locE/locK","locF/locG"};

        var folders = Folder.Parse(strings);

        folders.Dump();
    }


    public class Folder
    {
        public string Name { get; set; }

        public List<Folder> Folders { get; internal set; }

        public Folder()
        {
            Folders = new List<Folder>();
        }
        //Presume that each string will be folder1/folder2/folder3
        public static IEnumerable<Folder> Parse(IEnumerable<string> locations)
        {
            var folders = new List<Folder>();
            foreach (string location in locations)
            {
                string[] parts = location.Split('/');
                Folder current = null;
                foreach (string part in parts)
                {
                    var useFolders = current != null ? 
                               current.Folders : folders;
                    current = useFolders.SingleOrDefault(f => f.Name == part) ?? new Folder() { Name = part };
                    if (!useFolders.Contains(current)) { useFolders.Add(current); }
                }
            }
            return folders;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文