I'm currently dynamically building a linq expression tree using just a type and property name with the code found here: "Dynamic LINQ OrderBy on IEnumerable<T>", see the accepted answer.
This works fine for most properties but I need a custom sort order when the property type is an enum, e.g. sorting by a string representation of the value, which can be different depending on language.
E.g. I have a list of vehicle types.
public enum VehicleType : int
{
Car = 1,
Train = 2,
Bike = 3,
etc...
}
In English the name's would be "Car", "Bike" and "Train" but in French they would be "Voiture", "Train" and "Vélo", so obviously the sort order would be different.
The expression built must be compatible with NHibernate (Version 3.3.1.4000). There may be sorts added before and after the enum sort.
I have the list (or array) of the enums ordered correctly for the given language e.g. in English the list would be ordered "Bike", "Car" and "Train" and in French "Train", "Bike" and "Car", which translates into a required order of 3, 1, 2 or 2, 3, 1.
The translation of an enum value can't be accessed from within the database. E.g. I can't lookup the name for a given value and language.
Essentially what I need is a way of ordering the search results by the index of the enum value within my sorted list.
Is this possible?