2

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?

Community
  • 1
  • 1
Dasmo
  • 31
  • 4

3 Answers3

0

You can get the int value of an enum item as follows:

int enumItemValue = (int)YourEnum.EnumItem1;

use that integer value to order your list

How to define the order for each language

Create a custom Attribute that holds the order of the enum items for each language. See: http://msdn.microsoft.com/en-us/library/sw480ze8.aspx

sjkm
  • 3,706
  • 2
  • 22
  • 42
  • This doesn't answer the question, as I don't want a straight ascending or descending sort. E.g I need to sort the results in value order 3, 1, 2 or 2, 3, 1 depending on the users language. – Dasmo Apr 04 '14 at 08:04
  • I've added an explanation – sjkm Apr 04 '14 at 09:19
0

So building sjkm's answer you should be able to do it like this.

VehicleList.OrderBy(Vehicle => (int)Vehicle.VehicleType);

Also you would need to make sure that you have a using statment for System.Linq.

Brandon Johnson
  • 523
  • 3
  • 11
  • Please see "http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet" (And the accepted answer). At the point I generate my order by expression all I have is a type and property name. If I could do the above it would be pretty straight forwards... – Dasmo Apr 04 '14 at 08:05
0

I have ended up using the System.Linq.Dynamic code Microsoft wrote to build the order by as a string.

E.g. "VehicleType = Bike ? 0 : 1, VehicleType = Car ? 0 : 1, VehicleType = Train ? 0 : 1"

I then pass that to the order by extension method which will parse it into the correct expression tree (E.g. the bit I couldn't figure out).

For anyone whom is interested you can download the System.Linq.Dynamic code from here: http://msdn.microsoft.com/en-US/vstudio/bb894665.aspx, or someone else has added it as a package on NuGet.

Dasmo
  • 31
  • 4