У меня возникли проблемы с выяснением того, как эффективно сортировать список родительских элементов на основе дочерних элементов.Список сортировки родительских/дочерних объектов
Я не могу просто сортировать дочерние элементы. Мне нужен результат сортировки дочерних элементов, чтобы повлиять на сортировку родительского списка.
По существу, я пытаюсь сортировать Родителей в порядке, отражающем имя их детей в порядке убывания.
Есть ли способ «linqish» сделать это, как только у меня уже есть список родителей в памяти? Если это так, любая помощь, которую вы могли бы себе позволить, была бы большой.
Вот пример ....
//What I am trying to do is to figure out how to sort the order of parent1, parent2, parent3
//based on the names of their children.
//More specifically, the expected output would be:
//parent 1 (because she has a child with the name of Zoey),
//parent 3 (because she has a child next in desc order with the name of Yolanda),
//parent 2 (because her child names in desc order would Matt).
public class Parent
{
public int id { get; set; }
public int SortOrder { get; set; }
//some properties
public List<Child> Children { get; set; }
public static List<Parent> GetSortedParentsByChildName()
{
List<Parent> myUnsortedList = new List<Parent>()
{
new Parent()
{
id = 1,
Children = new List<Child>()
{
new Child(1, "Billy"),
new Child(1, "Zoey"),
new Child(1, "Robert"),
}
},
new Parent()
{
id = 2,
Children = new List<Child>()
{
new Child(1, "Gabe"),
new Child(1, "Matt"),
new Child(1, "Alyssa"),
}
},
new Parent()
{
id = 3,
Children = new List<Child>()
{
new Child(1, "Will"),
new Child(1, "Bob"),
new Child(1, "Yolanda"),
}
},
};
return myUnsortedList; //.OrderBy(my actual question);
}
}
public class Child
{
public int id { get; set; }
//some properties
public string Name { get; set; }
public Child(int id, string Name)
{
this.id = id;
this.Name = Name;
}
}
Любой образец ввода \ вывода, который вы ожидаете? –
Список mySortedParents = ????? Какой магический способ сортировать всех родителей по имени детей по убыванию ??? –
Solo812
Для столбца SortOrder? –