1

假设以下模型。请注意自引用关系“parent”。排序自参考关系

public class Category 
    { 
     public virtual long Id { get; set; } 
     public virtual string Name { get; set; } 
     public virtual Category Parent { get; set; } 
     public virtual long? ParentId { get; set; } 
    } 

我的数据如下:

id | name | parentId 
1--------tag 1 ----- null 
2--------tag 2 ----- 1 
3--------tag 3 ----- 1 
4--------tag 4 ----- 2 
5--------tag 5 ----- null 
6--------tag 6 ----- null 

我想写的是将数据排序的查询如下

tag 1 
----->tag 2 
----->----->tag 4 
----->tag 3 
tag 5 
tag 6 

这是我的代码

var categorys = __categories 
       .AsNoTracking() 
       .ToList(); 

我不知道如何分类

+0

搜索有关[树数据结构(http://stackoverflow.com/questions/9860207/build-a-simple-high-performance-tree-data-structure- in-c-sharp) – Sakura

回答

0

试试这个递归函数

class Program 
{ 
    static void Main(string[] args) 
    { 
     using (var db = new aaContext2()) 
     { 
      Temp temp = new Temp(); 

      var cc = db.Catagory.FirstOrDefault(); 
      IList<Category> parentList =new List <Category>(); 
      foreach (Category catagory in db.Catagory.Where(cat => cat.ParentId == null)) 
      { 
       parentList.Add(temp.Recursive(catagory.Id, catagory.Name)); 
      } 
     } 
    } 
} 
public class Temp{ 
    public Category Recursive(long parentId, string name) 
    { 
     Category catagory = new Category(); 
     catagory.Id = parentId; catagory.Name = name; 
     using (var db = new aaContext2()) 
     { 
      //base condition 
      if (db.Catagory.Where(catagory1 => catagory1.ParentId == parentId).Count() < 1) 
      { 
       return catagory; 
      } 
      else 
      { 
       IList<Category> newCatagoryList = new List<Category>(); 
       foreach (Category cat in db.Catagory.Where(cata => cata.ParentId == parentId)) 
       { 
        newCatagoryList.Add(Recursive(cat.Id, cat.Name)); 
       } 
       catagory.CatagoryList = newCatagoryList; 
       return catagory; 
      } 
     } 
    } 
} 
public class aaContext2 : DbContext 
{ 
    public DbSet<Category> Catagory { get; set; } 
} 
public class Category 
{ 
    public virtual long Id { get; set; } 
    public virtual string Name { get; set; } 
    public virtual Category Parent { get; set; } 

    public virtual ICollection<Category> CatagoryList { get; set; } 
    public virtual long? ParentId { get; set; } 
} 
+1

谢谢。优秀的解决方 – testStack201541

1

那么我会更多地描述层次组织而不是排序,但这里有一个很简单的例子。请注意,这是不是很为寻找每个父Category优化可能需要整个Category列表进行全面扫描,但它是一个很好的起点:即根据

using System; 
    using System.Collections.Generic; 
    using System.Linq; 

    namespace SimpleTree 
    { 
     public class Program 
     { 
      private static void Main(string[] args) 
      { 
       var categories = new List<Category>() 
       { 
        new Category {Id = 1, Name = "tag 1"}, 
        new Category {Id = 2, Name = "tag 2", ParentId = 1}, 
        new Category {Id = 3, Name = "tag 3", ParentId = 1}, 
        new Category {Id = 4, Name = "tag 4", ParentId = 2}, 
        new Category {Id = 5, Name = "tag 5"}, 
        new Category {Id = 6, Name = "tag 6"}, 
       }; 

       foreach (var category in categories) 
       { 
        category.Parent = FindParent(categories, category.ParentId); 
       } 

       //pretty printing with indentation is left as an exercise for you :) 
       foreach (var category in categories) 
       { 
        Console.WriteLine("ID:{0} Name:{1} ParentID:{2}", category.Id, category.Name, category.ParentId); 
       } 
       Console.ReadLine(); 
      } 

      private static Category FindParent(IEnumerable<Category> categories, long? parentId) 
      { 
       if (parentId == null) return null; 
       return categories.FirstOrDefault(c => c.Id == parentId); 
      } 
     } 


     public class Category 
     { 
      public virtual long Id { get; set; } 
      public virtual string Name { get; set; } 
      public virtual Category Parent { get; set; } 
      public virtual long? ParentId { get; set; } 
     } 
    } 

输出

ID:1 Name:tag 1 ParentID: 
ID:2 Name:tag 2 ParentID:1 
ID:3 Name:tag 3 ParentID:1 
ID:4 Name:tag 4 ParentID:2 
ID:5 Name:tag 5 ParentID: 
ID:6 Name:tag 6 ParentID: 

注在你的用例中,你可能会发现在Category对象上包含一个ChildCategories集合很有用,并且也可以填充它,这样就很容易在任何方向上走树。