niks
niks

Reputation: 659

Sorting collection

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

Answers (2)

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6023

you can cast your EntityData attribute to StructuralPart.

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

Upvotes: 0

Rudis
Rudis

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

Related Questions