我有一个小菜单,我想通过DB表ID来订购,如下所示:
public class Obj
{
public string Value { get; set; }
public int? ParentNodeId { get; set; }
public int? PreviousNodeId { get; set; }
public int? NextNodeId { get; set; }
}
private IEnumerable<MenuNodeDTO> GetSortedMenuNodes(int menuId)
{
var nodes = LoadMenuNodes(menuId);
foreach (MenuNode menuNode in nodes
.Where(s => s.MenuItemId == null && s.ParentNodeId == null)
.OrderBy(x => x?.PreviousNodeId)
.Where(x => x.PreviousNodeId != x.Id))
{
MenuNodeDTO tmpMenuNode = new MenuNodeDTO();
tmpMenuNode.MenuNodeDtoId = menuNode.Id;
tmpMenuNode.MenuItemCode = menuNode.MenuItem?.Code;
tmpMenuNode.ChildMenuNodes = GetChildNodes(menuNode).ToList();
tmpMenuNode.MenuSettings = GetMenuSettings(menuNode).ToList();
yield return tmpMenuNode;
}
}
示例:
Id = 1 MainMenuPoint1 ParentNodeId = null, PreviousNodeId = null, NextNodeId = 4
Id = 2 -> 1 ChildNodeFromPoint1 ParentNodeId = 1, PreviousNodeId = null, NextNodeId = null
Id = 3 --> ChildNodeFromFirstChildNode1 ParentNodeId = 2, PreviousNodeId = null, NextNodeId = null
Id = 4 MainMenuPoint2 ParentNodeId = null, PreviousNodeId = 1, NextNodeId = null
我对每个人的问题是,当id改变时,顺序不起作用。 有人知道如何通过parentid,previousid和next id进行排序,每次检查所有关系的id?
答案 0 :(得分:1)
id