I have collection of objects. I need to sort it by difficult condition, using many properties and calculations. My solution is to define in object special int property, that returns value, that OrderBy shoultd sort by. This solution is not good enough.
For example, collection should be sorted first by State property, then if State = 1, by Value1 property, if State = 2, by Value2 property and so on.
Advice, please, some solution.
I have collection of objects. I need to sort it by difficult condition, using
Share
You can order a collection by multiple criteria by using OrderBy for the first criterium and chaining the other criteria after that using ThenBy.
Your first criterium is to simply order by the State property. Your second criterium depends on the State property; assuming that Value1, Value2 and so on have the same type, you can use a
switchstatement to select the right value: