Reputation: 659
I would like to know how using Linq, I could sort the List if every member in List has EntityData property, which is a class:
List<Line> profilesLines = new List<Line>();
Line line = new Line(...);
line.EntityData = new StructuralPart { Guid = "123456", OtherPropertA = 2, Other PropertyB = 3};
I know how to sort a List if the properties after which sorting should be done are not inside a class:
List<Line> SortedList = profilesLines.OrderBy(ent => ent.EntityData).ToList();
But is it possible to make a statement in one line that would cast EnitityData to "StructuralPart" and then sort in based on properties defined in that class?
Upvotes: 1
Views: 74
Reputation: 6023
you can cast your EntityData
attribute to StructuralPart
.
List<Line> sortedList = profilesLines.OrderBy(ent => ((StructuralPart)ent.EntityData).Guid).ToList();
Upvotes: 0
Reputation: 1217
Do you mean something like this:
List<Line> SortedList = profilesLines.OrderBy(ent => ent.EntityData.Guid).ThenBy(ent => ent.EntityData.OtherPropertA).ToList();
You sort by Guid
and then by OtherPropertA
Upvotes: 3