2012-07-28 62 views
0

请看以下表结构:树结构得到父节点及其子

Folderid     parentFolderid    Guid 
1       0       1234 
2       1       5678 
3       2       9012 
4       3       87697 
5       7       4443 

的要求是,如果我通过folderId功能必须给我所有的GUID。

例如:如果我通过1函数,我应该得到前四个GUID(父项及其子项)。 我有如下返回所有的GUID的函数:

public List<Guid> Folders(int folderId) 
{ 
    // To get the folderids based on parentfolderid 
    var a = entity.Where(x => x.parentfolderId == folderId).FirstOrDefault(); 
    return a; 

} 

我能得到最多只能IDS的一个水平。

有没有什么办法让父母,孩子,孙辈到叶子?

+0

要高度重视其获得盛大的孩子们? – 2012-07-28 04:17:32

+0

是的,它甚至得到了granichildren,并且直到叶。我忘了提及那 – user1400915 2012-07-28 04:26:35

+0

你走了。对不起,必须查看答案。 – 2012-07-28 05:03:50

回答

0

如果你能够获得该表的一类,检查了这一点:

public class Entity 
{ 
    public int ID { get; set; } 
    public int ParentID { get; set; } 
    public string Name { get; set; } 

    public static List<Entity> GetTree(int ID, List<Entity> ListToSearch, bool First = true) 
    { 
     List<Entity> FilteredEntities = new List<Entity>(); 

     FilteredEntities.AddRange(ListToSearch.Where<Entity>(x => x.ParentID == ID).ToList<Entity>()); 

     List<Entity> Temp = new List<Entity>(); 
     foreach (Entity current in FilteredEntities) 
     { 
      Temp.AddRange(GetTree(current.ID, ListToSearch, false)); 
     } 

     FilteredEntities.AddRange(Temp); 

     if (First) 
     { 
      FilteredEntities.Add(ListToSearch.Where<Entity>(x => x.ID == ID).Single<Entity>()); 
     } 

     return FilteredEntities; 
    } 
} 

用法:

List<Entity> filteredEntities = Entity.GetTree(1, entities); 
    List<string> onlyTheNames = filteredEntities.Select<Entity, string>(x => x.Name).ToList<string>(); 

问候

0

如果使用此node class,你可以写这样的代码。

public class Folder 
{ 
    public int Id { get; set; } 
    public int? ParentId { get; set; } 
    public Guid SomeGuid { get; set; } 
} 

,并和例子什么可能的是:

var list = new List<Folder> 
{ 
    new Folder {Id = 0, ParentId = null, SomeGuid = new Guid("0000b25b-8538-4b78-818a-9094507e0000") }, 
    new Folder {Id = 1, ParentId = 0, SomeGuid = new Guid("1000b25b-8538-4b78-818a-9094507e0001") }, 
    new Folder {Id = 2, ParentId = 1, SomeGuid = new Guid("2000b25b-8538-4b78-818a-9094507e0002") }, 
    new Folder {Id = 3, ParentId = 1, SomeGuid = new Guid("3000b25b-8538-4b78-818a-9094507e0003") }, 
    new Folder {Id = 4, ParentId = 2, SomeGuid = new Guid("4000b25b-8538-4b78-818a-9094507e0004") }, 
    new Folder {Id = 5, ParentId = 3, SomeGuid = new Guid("5000b25b-8538-4b78-818a-9094507e0005") }, 
    new Folder {Id = 6, ParentId = 0, SomeGuid = new Guid("6000b25b-8538-4b78-818a-9094507e0006") }, 
    new Folder {Id = 7, ParentId = 4, SomeGuid = new Guid("7000b25b-8538-4b78-818a-9094507e0007") }, 
    new Folder {Id = 8, ParentId = 3, SomeGuid = new Guid("8000b25b-8538-4b78-818a-9094507e0008") }, 
}; 
var rootNode = Node<Folder>.CreateTree(list, n => n.Id, n => n.ParentId).Single(); 

var firstChild = rootNode.Children.First(); // Id 1 
var descendentsOfFirstChild = firstChild.Descendants; // All descendants of node 1