첫째, 그것은 단지 하나 개의 자식 기록을 가지고 있기 때문에이 코드가 작동하지 않는
Func<ChildObject, string> orderingFunction = (c => c.ChildRecords.FirstOrDefault().SomeProperty);
를 작동하지 않는 이유에 이유. 당신이 아이의 기록 bythe 순서에 상관없이이, 그것은 항상 X 기준에 따라 기본적으로 다시 주문해야하기 때문
results = results.OrderBy(x => x.ChildRecords.OrderBy(c =>
c.SomeProperty))
이
가 작동하지 않습니다.
그럼 어떻게 만드나요? 회사 목록을 주문하려면 논리 주문을 주문 기능에 넣기 만하면됩니다. 이 같은.
results = results.OrderBy(Function(m)
If "Check custom logic to determine the order" Then
Return 2
Else
Return 1
End If
End Function)
은 VB.NET 언어에 있습니다. 이것에 대한 사과. 나는 C#의 문법에 대해 확신하지 못한다. 그러나 그 아이디어는 대략 같다. 샘플 케이스에 대한
, 클래스의 구조는 다음과 같습니다 :
Parent p1 = new Parent
{
Name = "Test1",
Index = "1",
Children = new List<Child> {
new Child {Name = "Child1", Index = "3"},
new Child {Name = "Child2", Index = "2"},
new Child {Name = "Child3", Index = "1"}
}
};
Parent p2 = new Parent
{
Name = "Test2",
Index = "2",
Children = new List<Child> {
new Child {Name = "Child4", Index = "6"},
new Child {Name = "Child5", Index = "5"},
new Child {Name = "Child6", Index = "4"}
}
};
List<Parent> listParent = new List<Parent>();
listParent.Add(p1);
listParent.Add(p2);
그래서, 당신이 달성하고자하는 것은 선택 하위 속성을 분류하는 것입니다?
나는 이렇게 추측합니까? 이것은 아동에 따라 주문할 수 있습니다.
listParent.ForEach(x => {
x.Children = x.Children.OrderBy(y => y.Index).ToList();
});
listParent.ToList<Parent>().ForEach(x => x.Children.ToList<Child>().ForEach(y => Console.WriteLine(y.Name)));
첫 번째 접근 방식은 각 회사의 첫 번째 ChildRecord에서 SomeProperty 값으로 정렬됩니다. 그 접근법에 만족하지 못한 것은 무엇입니까? –
@SteveRuble - 회사가 하나의 하위 레코드 만 갖고 있지만 정렬해야하는 하위 레코드가 여러 개있는 경우에는 괜찮습니다. 그리고 말한 것처럼이 접근법을 사용하여 첫 번째 ChildRecord 만 정렬됩니다. – markpsmith