I have a class called NavigationElement
that looks like this
public class NavigationElement
{
public int Id { get; set; }
public string Title { get; set; }
public string Link { get; set; }
public int SortOrder { get; set; }
public bool Visible { get; set; }
public int? ParentId { get; set; }
public virtual ICollection<NavigationElement> Children { get; set; }
public virtual NavigationElement Parent { get; set; }
public NavigationElement()
{
Children = new List<NavigationElement>();
}
}
As you can see, the class is self referencing. From that, I am creating a site navigation menu with drop downs (hierarchy in play).
I am struggling in the ordering of the items. I want the top-level items to be ordered by the SortOrder
property, but everything underneath, I would like ordered alphabetically by the Title
property.
Here is why I have done so far.
var orderedModel = unorderedModel.OrderBy(x => x.SortOrder).ThenBy(x => x.Children.OrderBy(y => y.Title).ThenBy(z => z.Children.OrderBy(a => a.Title))).ToList();
unorderedModel
is of type List<NavigationElementModel>
.
This is compiling, but I get an error when I run the code. The error says:
At least one object must implement IComparable.