I have a small menu that I would like to order by the identifier of the database table as follows:
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; } }
Example:
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
The problem with mine for everyone is that when changing the identifier, the order does not work. Does anyone have an idea how to sort by parentid, previousid and next id, which check every time all identifiers for relations?
source share