困难的分层排序

kgs*_*gst 5 c# sql linq sorting

我遇到了一个非常困难的排序问题,我想知道是否有人可以帮我解决这个问题.基本上我有一个充满以下信息的SQL表:

ID (The comment's Unique Identifier)

Previous ID (The ID of the comment that is being replied to with this comment)

Position (The position of how "deep" the comment is, a post directly on a 
page would be "1" a reply to that "2", etc.
Run Code Online (Sandbox Code Playgroud)

是否可以使用此信息对C#/ LINQ进行排序,使其在调用时以正确的顺序返回?

一个例子可能如下:

ID | Position | PreviousID | Message|

1  | 1        | 0          | Hello
2  | 1        | 0          | How
3  | 2        | 1          | There!
4  | 2        | 2          | Are
5  | 3        | 4          | You?
Run Code Online (Sandbox Code Playgroud)

将按以下顺序排序:

1. Hello
2. There!
3. How
4. Are
5. You?
Run Code Online (Sandbox Code Playgroud)

我无法绕过如何做到这一点,或者甚至可能做到这一点,所以我非常感谢即使只是朝着正确的方向轻推,谢谢!

而且只是为了获得更多信息,这是一个包含大量内容的现有表格,无法删除,我只需要找到一种方法以这种方式对其进行排序.

Cod*_*ely 2

LINQ 可以使用分层连接对此进行建模

这里是C# 和 LINQ 中的递归层次连接的示例,并给出了一个简单的演练,可以完成您想要的操作。

键略有不同,但您应该能够映射到示例中。