Does anyone have an extension method to quickly convert the types in a LinkedList<T>
using a Converter<TInput, TOutput>
?
I'm a bit surprised, where is the ConvertAll<TOutput>(delegate)
?
Does anyone have an extension method to quickly convert the types in a LinkedList<T>
using a Converter<TInput, TOutput>
?
I'm a bit surprised, where is the ConvertAll<TOutput>(delegate)
?
The ConvertAll
equivalent in extension methods for Linq is called Select
!
var result = myLinkedList.Select(x => FancyCalculationWith(x))
Depends on what you want to get out of it, but you can use Cast then enumerate through the resulting IEnumerable.
public class Foo
{
...
}
public class Bar : Foo
{
...
}
var list = new LinkedList<Bar>();
.... make list....
foreach (var foo in list.Cast<Foo>())
{
...
}
As tvanfosson says it is possible to Cast<T>
but if you want to avoid an InvalidCastException
you can use the OfType<T>
extension method which will silently pass over and items in the list that fail the conversion to the type of the generic type parameter you supply.