C# 分类收集

C# 分类收集,c#,linq,sorting,C#,Linq,Sorting,我想知道如何使用Linq,如果列表中的每个成员都有EntityData属性,我可以对列表进行排序,该属性是一个类: List<Line> profilesLines = new List<Line>(); Line line = new Line(...); line.EntityData = new StructuralPart { Guid = "123456", OtherPropertA = 2, Other PropertyB = 3}; List profi

我想知道如何使用Linq,如果列表中的每个成员都有EntityData属性,我可以对列表进行排序,该属性是一个类:

List<Line> profilesLines = new List<Line>();
Line line = new Line(...);
line.EntityData = new StructuralPart { Guid = "123456", OtherPropertA = 2, Other PropertyB = 3};
List profilesLines=new List();
行=新行(…);
line.EntityData=newstructuralpart{Guid=“123456”,OtherPropertA=2,otherpropertyb=3};
我知道如果应该进行排序的属性不在类中,如何对列表进行排序:

List<Line> SortedList = profilesLines.OrderBy(ent => ent.EntityData).ToList();
List-SortedList=profilesLines.OrderBy(ent=>ent.EntityData.ToList();

但是,是否可以在一行中创建一个语句,将EntityData强制转换为“StructuralPart”,然后根据该类中定义的属性进行排序?

您的意思是这样的:

List<Line> SortedList = profilesLines.OrderBy(ent => ent.EntityData.Guid).ThenBy(ent => ent.EntityData.OtherPropertA).ToList();
List-SortedList=profilesLines.OrderBy(ent=>ent.EntityData.Guid).ThenBy(ent=>ent.EntityData.OtherPropertA).ToList();

您可以按
Guid
排序,然后按
OtherPropertA

排序,您可以将
EntityData
属性强制转换为
StructuralPart

List<Line> sortedList = profilesLines.OrderBy(ent => ((StructuralPart)ent.EntityData).Guid).ToList();
List sortedList=profilesLines.OrderBy(ent=>((StructuralPart)ent.EntityData.Guid).ToList();

您是否尝试过在lambda中铸造零件?这就是我要找的!非常感谢。