我有一个函数返回IDictionary<TKey, List<TValue> >
。
我有另一个功能,需要一个IDictionary<TKey, IEnumerable<TValue> >
。如何将IDictionary <TKey,列表<TValue>>转换为IDictionary <TKey,IEnumerable <TValue>>?
我需要将第一个函数的返回传递给第二个函数。
编译器不想隐式地将第一个转换为第二个。那么如何将第一个转换为O(1)中的第二个呢?
我总是可以写一个转换功能
public static IDictionary<TKey, IEnumerable<TValue>> ToIEnumerable<TKey, TValue>(this IDictionary<TKey, List<TValue>> source)
{
return source.ToDictionary(s => s.Key, s => s.Value.AsEnumerable());
}
但我敢肯定这是不是O(1),更像是O(log n)的。
只是好奇,你想要做什么的目的是什么? –
嗯,能够通过做一个似乎没有必要的转换来调用我的第二个功能而不会浪费时间! –
如果你想O(1)。只需在它周围写一个包装。我没有看到这个问题,虽然'列表'已经是'IEnumerable '。转换在哪里? –
leppie