JIT exception with ToArray() Linq

Trying to compile to iOS and I’m getting the following exception:

ExecutionEngineException: Attempting to JIT compile method ‘(wrapper managed-to-managed) UnityEngine.RaycastHit:System.Collections.Generic.ICollection`1.CopyTo (UnityEngine.RaycastHit,int)’ while running with --aot-only.

I’ve done my research and I understand that JIT is “Just In Time”, and I need “Ahead Of Time”. I know that it’s to do with the declaration within my line of code. What I want to know is how I can stop the ToArray() throwing an exception and declare the type?

var hitPoints : RaycastHit[] = Physics.RaycastAll(ray, rayLength, layerMask2).OrderBy(function(h){ return h.distance; }).ToArray();

ToArray(RaycastHit) as RaycastHit;

ToArray() is a Linq method.

Most of Linq extension methods from Linq for Collections are not working with IEnumerables on iOS since they require AOT runtime compiler which is not supported.

However there is a Linq for iOS library on Asset Store that is similar to Linq but doesn’t require a runtime compiler. So you can use it on iOS and ToArray() method will work with this library.