I have two same enums that have the same names of members, but they are in different namespaces so they are "different types", but in fact namespace1.enum1 {a,b,c,d,e,f} and namespace2.enum2 {a,b,c,d,e,f}
How is the easiest way to convert IEnumerable<enum1>
to List<enum2>
without using loops?
Well something's going to loop somewhere, but it doesn't have to be in your code. Just a LINQ Select
will be fine:
var result = original.Select(x => (Enum2) x).ToList();
Or you can use Cast
(still O(n) :-))
var result = original.Cast<int>().Cast<Enum2>().ToList();
Warning
It seems that this will not always work as expected (InvalidCastException
can be thrown in some cases).
See comments to figure out why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With