Sorting 基于子属性的Linq动态排序
基于这个示例,我能够对父记录属性应用排序。但是,我不能将排序应用于子记录 在本例中,创建了一个委托,该委托设置公司属性的顺序:Sorting 基于子属性的Linq动态排序,sorting,datatable,linq-to-entities,Sorting,Datatable,Linq To Entities,基于这个示例,我能够对父记录属性应用排序。但是,我不能将排序应用于子记录 在本例中,创建了一个委托,该委托设置公司属性的顺序: Func<Company, string> orderingFunction = (c => sortColumnIndex == 1 ? c.Name : sortColumnIndex == 2 ? c.Address :
Func<Company, string> orderingFunction = (c => sortColumnIndex == 1 ? c.Name :
sortColumnIndex == 2 ? c.Address :
c.Town);
现在,假设该公司有子记录,我将如何使用相同的技术对子属性进行排序
我试过这样的方法:
Func<ChildObject, string> orderingFunction = (c => c.ChildRecords.FirstOrDefault().SomeProperty );
这两种方法都不起作用。首先,它不起作用的原因是什么
Func<ChildObject, string> orderingFunction = (c => c.ChildRecords.FirstOrDefault().SomeProperty );
这不起作用,因为无论您按子记录排序,默认情况下都会根据x标准将其重新排序
那么,如何让它工作呢?
如果您想对公司列表进行排序,只需将逻辑排序放在排序函数中即可。像这样的
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);
Parent p1=新的父级
{
Name=“Test1”,
Index=“1”,
Children=新列表{
新子项{Name=“Child1”,Index=“3”},
新子项{Name=“Child2”,Index=“2”},
新子项{Name=“Child3”,Index=“1”}
}
};
父级p2=新父级
{
Name=“Test2”,
Index=“2”,
Children=新列表{
新子项{Name=“Child4”,Index=“6”},
新子项{Name=“Child5”,Index=“5”},
新子项{Name=“Child6”,Index=“4”}
}
};
List listParent=新列表();
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)));
listParent.ForEach(x=>{
x、 Children=x.Children.OrderBy(y=>y.Index.ToList();
});
listParent.ToList().ForEach(x=>x.Children.ToList().ForEach(y=>Console.WriteLine(y.Name));
您的第一种方法将根据每家公司的第一个ChildRecord上SomeProperty的值进行排序。这种方法有什么不令人满意的地方?@SteveRuble-如果公司只有一个子记录,但有多个子记录需要排序,并且正如您所说,只有第一个子记录使用这种方法进行排序,那就好了。感谢您解释为什么它不起作用,但是我在选择可排序的子属性tanks的Linq语法方面遇到了问题,请澄清。请参考我上面的评论。所以,您想要实现的是对选定的子属性进行排序?是的,这是正确的,我需要对子属性进行排序。我尝试了你的例子,它几乎奏效了——它在子属性上排序,但按父属性分组。我想在没有父对象分组的情况下按子属性排序。我认为如果不在两个父对象之间进行连接,然后对子对象进行排序,这是不可能的。order by函数用于对集合进行排序。例如,orderby函数将对父集合中的子集合进行排序。这就是为什么子属性按父属性排序但分组的原因。
results = results.OrderBy(Function(m)
If "Check custom logic to determine the order" Then
Return 2
Else
Return 1
End If
End Function)
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)));