Question
Dim classCodeDetails As List(Of ClassCodeDetail) =
db.ClassCodeHeaders.Single(Function(cch)
cch.CLCH_ID = classCodeHeaderId
).ClassCodeDetails.ToList()
classCodeDetails.Sort(Function(c1, c2)
c1.Make.MAKE_English.CompareTo(c2.Make.MAKE_English)
)
My question is how can I sort on multiple attributes? I want to sort first by Make.MAKE_English
, then By Model.MODL_English
.
How do I implement that?
Thanks,
~ck
Solution
If you don't need to sort in-place, you could use the OrderBy
function:
Dim sortedList = list.OrderBy(Function(x) x.Prop1).ThenBy(Function(x) x.Prop2)
Your example would be:
Dim classCodeDetails As List(Of ClassCodeDetail) = _
db.ClassCodeHeaders.Single(Function(cch) cch.CLCH_ID = classCodeHeaderId).ClassCodeDetails _
.OrderBy(Function(c1) c1.Make.MAKE_English) _
.ThenBy(Function(c1) c1.Make.MODL_English) _
.ToList()
In fact, this is the correct way to do this (seems you are using LINQ to SQL) since it'll use ORDER BY
in the generated SQL statement to sort data instead of manually sorting it on the client.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow