【发布时间】:2010-10-18 15:27:57
【问题描述】:
有没有人有扩展方法来使用Converter<TInput, TOutput> 快速转换LinkedList<T> 中的类型?
我有点惊讶,ConvertAll<TOutput>(delegate)在哪里?
【问题讨论】:
标签: c# .net vb.net linked-list converter
有没有人有扩展方法来使用Converter<TInput, TOutput> 快速转换LinkedList<T> 中的类型?
我有点惊讶,ConvertAll<TOutput>(delegate)在哪里?
【问题讨论】:
标签: c# .net vb.net linked-list converter
Linq 扩展方法中的 ConvertAll 等效项称为 Select!
var result = myLinkedList.Select(x => FancyCalculationWith(x))
【讨论】:
取决于你想从中得到什么,但你可以使用 Cast 然后枚举生成的 IEnumerable。
public class Foo
{
...
}
public class Bar : Foo
{
...
}
var list = new LinkedList<Bar>();
.... make list....
foreach (var foo in list.Cast<Foo>())
{
...
}
【讨论】:
作为tvanfosson says,可以使用Cast<T>,但如果您想避免InvalidCastException,您可以使用OfType<T> 扩展方法,该方法将静默传递列表中无法转换为类型的项目您提供的泛型类型参数。
【讨论】: