Optimizing casting objects of a .NET List object

We are on the Wolfram Finance Platform using NET/Link to grab data from a C# API. We have a list of Core.Data.Securities.Options that we need to upcast to Core.Data.Securities.Security. Working with some great people on here yesterday, we were able to piece together how to cast the .NET list. However, it takes about 41 seconds.

Current Code

options =
NETNew["System.Collections.Generic.List1[Core.Data.
Securities.Security]"];

Scan[options@
"Core.Data.Securities.Security"]] &,
optionChain@Options@ToArray[]];

client@GetMarketDataSnapshots[options];


optionChain@Options gets a System.Collections.Generic.List1[Core.Data.Securities.Option] object (this can be very, very long). Our GetMarketDataSnapshots only accepts a System.Collections.Generic.List1[Core.Data.Securities.Security] object.

We tried some other methods of casting before finding the above working code.

Examples/Failures

1

Map[CastNETObject[#, "Core.Data.Securities.Security"]&, optionChain@Options@ToArray[]];


Could not cast System.Collections.Generic.List1[Core.Data.Securities.Option] to System.Collections.Generic.List1[Core.Data.Securities.Security].

This baffled me for a long time, because it's not an unreasonable cast. I believe it had something to do with trying to put Security objects back into the List. Creating a new List object, like in the current code, squelched the error.

2

CastNETObject[optionChain@Options, "System.Collections.Generic.List1[Core.Data.Securities.Security]"]


CastNETObject::netexcptn: A .NET exception occurred: System.InvalidCastException: Specified cast is not valid. at Wolfram.NETLink.Internal.CallPacketHandler.cast(KernelLinkImpl ml). >>

I didn't really expect this one to work. It was during the desperate times. Adding it here, in case anyone has the same idea.

End Goal

Find a way to cast objects in a .NET list in Wolfram in the fastest way possible.

After thinking about it for a while, I realized that the best, least messy, and fastest solution was to create a helper library for situations like this.

namespace CastHelper
{
public class CastList
{
public static List<Security> CastListToSecurity(List<Option> castee)
{
List<Security> caster = castee.Select(x => x as Security).ToList();

return caster;
}
}
}


options = CastListCastListToSecurity[optionChain@Options]
`