1

I have a collection of entities.

One of the properties is an enumeration.

How can I sort the collection alphabetically by its enum name instead of value?

In LINQ it would look like this:

var a = collection.OrderBy(x => Enum.GetName(x.OrderType.GetType(), x.OrderType)).ToList();

I can't seem to translate this to Dynamic Linq.

I tried to put it directly, but it throws an Exception:

collection.OrderBy("x => Enum.GetName(x.OrderType.GetType(), x.OrderType) ascending")
MiBuena
  • 451
  • 3
  • 22
  • Try get the enum name outside of the dynamic linq and just put `OrderBy($"x => {enumName} asc")` – Eldar Sep 27 '22 at 12:43
  • 3
    Is there a reason you need this to be in dynamic linq? Also, if you say it throws an exception, then you should tell us what that exception is. – DavidG Sep 27 '22 at 12:50
  • We are researching different libraries for filtering and sorting, to avoid writing it ourselves. OData and Dynamic Linq are among the choices. The Exception message is "Enum type 'Enum' not found" @DavidG – MiBuena Sep 27 '22 at 12:56
  • So instead of `Enum.GetName` use `System.Enum.GetName`? – DavidG Sep 27 '22 at 12:58
  • @DavidG, just tried it. This time the exception message is: "Enum value 'GetName' is not defined in enum type 'System.Enum'" – MiBuena Sep 27 '22 at 13:02

1 Answers1

1

For .net framework (not core), see this answer: Call function in dynamic linq which explains that only certain types are available in dynamic linq expressions. Full list of types available in source code

For the dotnet core answer, see the documentation at https://dynamic-linq.net/advanced-configuration . You need to create a custom type provider, than create a parser configuration and use that in your query.

Example below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
                    
public class Program
{   
    public class MyCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider
    {
        public override HashSet<Type> GetCustomTypes() =>
            new[] { typeof(System.Enum), typeof(System.ConsoleColor) }.ToHashSet();
    }
    
    public static void Main()
    {
        // example data source (using primitive data type)
        var listy = new List<System.ConsoleColor>();
        listy.Add(ConsoleColor.DarkBlue);
        listy.Add(ConsoleColor.Yellow);
        listy.Add(ConsoleColor.Cyan);
        listy.Add(ConsoleColor.White);

        // regular linq query
        Console.WriteLine(String.Join(", ", listy.OrderBy(x => Enum.GetName(x.GetType(), x))));
        
        var config = new ParsingConfig
        {
            CustomTypeProvider = new MyCustomTypeProvider()
        };
        
        // dynamic linq calling Enum method
        Console.WriteLine(String.Join(", ", listy.AsQueryable().OrderBy(config, "x => Enum.GetName(x.GetType(), x)")));
    }
}

output

Cyan, DarkBlue, White, Yellow
Cyan, DarkBlue, White, Yellow
BurnsBA
  • 4,347
  • 27
  • 39