C# 如何按父级对数据库记录进行排序

C# 如何按父级对数据库记录进行排序,c#,sql,asp.net,linq,sorting,C#,Sql,Asp.net,Linq,Sorting,我的DB表是这样的: ID Name ParentID isActive 1 ABC NULL true 2 DEF 1 true 3 GHI 1 true 4 JKL NULL true 5 MNO 4 true 6 PRS NULL true 7 TUV NULL

我的DB表是这样的:

ID    Name    ParentID    isActive
1      ABC   NULL         true
2      DEF   1            true
3      GHI   1            true
4      JKL   NULL         true
5      MNO   4            true
6      PRS   NULL         true
7      TUV   NULL         true
8      WX    1            true
9      YZ    4            true
10     abc   7            true
我把它列在一个清单上:

var projectList = connection.Where(d=>d.Active);
当我把这个表放到一个列表中时,我想按父ID对它们进行排序。所以,排序后的列表应该是这样的:(每一行都替换了它下面的父行)

  • 1是2、3和8的父代

  • 4是5和9的父母


如何对该列表进行排序?

您只需通过区分父元素和子元素将结果与它自身连接起来即可。我希望这有帮助

static void Main(string[] args)
{
  var connection = new List<User>()
  {
    new User(1, "ABC", null, true),
    new User(2, "DEF", 1, true),
    new User(3, "GHI", 1, true),
    new User(4, "JKL", null, true),
    new User(5, "MNO", 4, true),
    new User(6, "PRS", null, true),
    new User(7, "TUV", null, true),
    new User(8, "WX", 1, true),
    new User(9, "YZ", 4, true),
    new User(10, "abc", 7, true)
  };


  var filtered = connection.Where(x => x.Active).OrderBy(z => z.ParentID);
  var result = filtered.Where(p => p.ParentID == null).GroupJoin(filtered, parent => parent.ID,
      child => child.ParentID,
      (parent, child) => new {Child = child, Parent = parent})
    .Aggregate(new List<User>(), (outList, tempObj) =>
    {
      outList.Add(tempObj.Parent);
      outList.AddRange(tempObj.Child);       
      return outList;
    });

  foreach (var item in result)
  {
    Console.WriteLine($"{item.ID} | {item.Name} | {item.ParentID}");
  }
}
static void Main(字符串[]args)
{
var连接=新列表()
{
新用户(1,“ABC”,null,true),
新用户(2,“定义”,1,正确),
新用户(3,“GHI”,1,正确),
新用户(4,“JKL”,null,true),
新用户(5,“MNO”,4,正确),
新用户(6,“PRS”,null,true),
新用户(7,“TUV”,空,真),
新用户(8,“WX”,1,正确),
新用户(9,“YZ”,4,正确),
新用户(10,“abc”,7,正确)
};
var filtered=connection.Where(x=>x.Active).OrderBy(z=>z.ParentID);
var result=filtered.Where(p=>p.ParentID==null).GroupJoin(filtered,parent=>parent.ID,
child=>child.ParentID,
(父,子)=>new{child=child,parent=parent})
.Aggregate(新列表(),(大纲视图,临时对象)=>
{
outList.Add(tempObj.Parent);
outList.AddRange(tempObj.Child);
返回最长时间;
});
foreach(结果中的var项目)
{
Console.WriteLine($“{item.ID}{item.Name}{item.ParentID}”);
}
}
它产生以下输出:

1 | ABC|
2 | DEF | 1
3 | GHI | 1
8 | WX | 1
4 | JKL|
5 | MNO | 4
9 | YZ | 4
6 | PRS|
7 | TUV|

10 | abc | 7

是否要使用linq或sql进行排序?需要在列表中使用OrderBy方法。也许<代码>按ID排序我使用Linq。你有答案吗?(父级->子级,其他父级->它的子级)。所以我想在父行之后列出子行。
static void Main(string[] args)
{
  var connection = new List<User>()
  {
    new User(1, "ABC", null, true),
    new User(2, "DEF", 1, true),
    new User(3, "GHI", 1, true),
    new User(4, "JKL", null, true),
    new User(5, "MNO", 4, true),
    new User(6, "PRS", null, true),
    new User(7, "TUV", null, true),
    new User(8, "WX", 1, true),
    new User(9, "YZ", 4, true),
    new User(10, "abc", 7, true)
  };


  var filtered = connection.Where(x => x.Active).OrderBy(z => z.ParentID);
  var result = filtered.Where(p => p.ParentID == null).GroupJoin(filtered, parent => parent.ID,
      child => child.ParentID,
      (parent, child) => new {Child = child, Parent = parent})
    .Aggregate(new List<User>(), (outList, tempObj) =>
    {
      outList.Add(tempObj.Parent);
      outList.AddRange(tempObj.Child);       
      return outList;
    });

  foreach (var item in result)
  {
    Console.WriteLine($"{item.ID} | {item.Name} | {item.ParentID}");
  }
}