简体   繁体   English

循环递归自联接表

[英]Loop recursive self join table

I have the following SQL Server table structure, it's a logical tree folder structure. 我有以下SQL Server表结构,它是一个逻辑树文件夹结构。

SQL Server表结构

I'm using Linq to SQL with SQL Server. 我正在使用Linq to SQL with SQL Server。

What I want to do is to loop and create on memory the folder structure, the hierarchical folder from Root, the main parent to all childs. 我想要做的是循环并在内存上创建文件夹结构,从Root的层次文件夹,所有子项的主要父项。

文件夹结构

I'm trying to do it with some foreach loops but I still don't get it. 我试图用一些foreach循环来做,但我仍然没有得到它。 I think I must use some kind of recursive way. 我想我必须使用某种递归方式。

Some query I tried: 我尝试了一些查询:

var flds = from folders in repo.Folders
                                   join folders2 in repo.Folders on folders.Id equals folders2.idParent
                                   select folders;

My goal is to iterate that folder logical structure. 我的目标是迭代该文件夹逻辑结构。

If you want to get folder as hierarchy given in your table, you can create it like this : 如果要将文件夹作为表中给出的层次结构,可以像这样创建它:

    var tableData = repo.Folders.ToList();
    var directories = tableData.Select(f => GetPath(f, table));

And as you asked, the recursive method for creating a folder hierarchy is : 正如您所问,创建文件夹层次结构的递归方法是:

    private static string GetPath(Folder f)
    {
        if (f.IdParent == 0)
        {
            return f.Name;
        }
        else
        {
            var parent = tableData.Find(d => d.Id == f.IdParent);
            return GetPath(parent) + "|" + f.Name;
        }
    }

You will get output like this : 你会得到这样的输出:

产量

It will give you a complete folder hirarchy, I don't know how you want to use it. 它会给你一个完整的文件夹hirarchy,我不知道你想如何使用它。 So you can do some modification as per your need. 所以你可以根据需要做一些修改。 Hope it helps! 希望能帮助到你!

Here I am adding the code to iterate and create the folder structure : 这里我添加代码来迭代并创建文件夹结构:

        foreach (var dir in directories)
        {
            var folders = dir.Split('|');
            var path = string.Empty;
            foreach (var folder in folders)
            {
                path = path + "\\" + folder;  // modify the path like '\\' if it is not valid I have not tested
                CreateFolder(path); // implement this method for actual creation of folders
            }
        }

And your method for creating folders : 以及创建文件夹的方法:

private static void CreateFolder(string directory)
    {
        var path = "C:" + directory; // add whenever you want to create structure
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    }

You could try this one: 你可以尝试这个:

// Get a list of the folders.
// We do this, in order to avoid the performance hit, that Panagiotis 
// pointed out correctly in his comment.
var folders = repo.Folders.ToList();

// Get the root folder.
var rootFolder = folders.Where(x=>x.idParent==null).SingleOrDefault();

// Group the folders by their parentId.
var groupedFolders = from folder in folders
                     where folder.idParent!=null
                     orderby folder.idParent ascending
                     group folder by folder.idParent into grp
                     select new
                     {
                         ParentFolderId = grp.Key,
                         Folders = grp.Select(x=>x)
                     };

// Print the name of the root folder.
Console.WriteLine("Root Folder", rootFolder.Name);

// Iterate through the groups of folders. 
foreach(var grp in groupedFolders)
{
    // Get the name of the parent folder for the current group.
    string parentFolderName = folders.Where(x=>x.Id==grp.ParentFolderId)
                                     .Single(x=>x.Name);


    // Print the name of the parent folder.
    Console.WriteLine("Parent Folder", parentFolderName);

    // Iterate through the folders of the current group.
    foreach(var folder in grp.Folders)
    {
        Console.WriteLine(folder.Name);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM