Search code examples
c#linqdynamicdynamic-linqdynamic-linq-core

Why is System.Linq.Dynamic.Core OrderBy not working with ImmutableArray?


I am trying to use System.Linq.Dynamic.Core with an ImmutableArray for dynamic OrderBy but I get the following exception:

Expression of type 'System.Collections.Immutable.ImmutableArray`1[$customType]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[$customType]' of method 'System.Linq.IOrderedEnumerable`1[$customType] OrderByDescending[$customType,Int32](System.Collections.Generic.IEnumerable`1[$customType], System.Func`2[$customType,System.Int32])' (Parameter 'arg0')

Why is this? Have they defined specific validations for Immutable types? Normal OrderBy is perfectly capable of sorting it. Should this be submitted to them as a feature request or am I missing something obvious? Once it is cast to a List it works like a charm.


Solution

  • This appears to be a bug in .Net (Core), specifically in the validation of arguments for Expression.Call.

    Ultimately the first parameter is validated by calling TypeUtils.AreReferenceAssignable and the code assumes that a value type cannot be (reference) assignable to a non-value type, and ImmutableArray is a value type because it is implemented with a struct.

    I opened an issue on github to see what others think.