我想知道如何使用Linq,如果List中的每个成员都有EntityData属性,我可以对List进行排序:这是一个类:
List<Line> profilesLines = new List<Line>();
Line line = new Line(...);
line.EntityData = new StructuralPart { Guid = "123456", OtherPropertA = 2, Other PropertyB = 3};
我知道如果要进行排序的属性不在类中,如何对List进行排序:
List<Line> SortedList = profilesLines.OrderBy(ent => ent.EntityData).ToList();
但是有可能在一行中生成一个语句,将EnitityData转换为“StructuralPart”,然后根据该类中定义的属性进行排序吗?
答案 0 :(得分:3)
你的意思是这样的:
List<Line> SortedList = profilesLines.OrderBy(ent => ent.EntityData.Guid).ThenBy(ent => ent.EntityData.OtherPropertA).ToList();
按Guid
排序,然后按OtherPropertA
答案 1 :(得分:0)
您可以将EntityData
属性投射到StructuralPart
。
List<Line> sortedList = profilesLines.OrderBy(ent => ((StructuralPart)ent.EntityData).Guid).ToList();