PedroC88
PedroC88

Reputation: 3829

Casting a list of an object to another with implicit conversion

I have two objects myObject1 and myObject2, in myObject1 I have the following method:

public static implicit operator myObject2(myObject1 param)
{ //Some Code }

How can I cast an IEnumerable<myObject1> to IEnumerable<myObject2>?

Upvotes: 0

Views: 68

Answers (2)

atlanteh
atlanteh

Reputation: 5835

var converted = original.Select(o=>(myObject2)o);

Upvotes: 1

Jacob
Jacob

Reputation: 78840

Try provoking a cast on each item:

IEnumerable<myObject1> original; 
// ...
IEnumerable<myObject2> converted = original.Cast<myObject2>();

Upvotes: 2

Related Questions