我的类实现了两次IEnumerable<T>
。 我怎样才能得到LINQ工作,而不是每次铸造hashtable
?当执行IEnumerable时,LINQ会感到困惑<T>两次
我写我自己的协变散列表实施方案也从.NET的IDictionary<TKey, TValue>
继承。最终,它实现了IEnumerable<T>
两次不同类型的T
。我隐式地实现了主要的可枚举接口,另一个明确地实现了。事情是这样的(伪):
class HashTable<TKey, TValue> :
...
IEnumerable<out IAssociation<out TKey, out TValue>>,
IEnumerable<out KeyValuePair<TKey, TValue>>
{
// Primary:
public IEnumerator<IAssociation<TKey, TValue>> GetEnumerator();
// Secondary:
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator();
}
当我foreach
哈希表,它需要as expected主枚举:
using System;
using System.Collections.Generic;
using System.Linq;
var hashtable = new HashTable<string, int>();
foreach (var kv in hashtable)
{
// kv is IAssociation<string, int>
}
现在我想要它做的LINQ同样的事情,但它甩编译器错误在我,因为它不知道挑的扩展方法哪个接口:
var xs1 = from x in hashtable // <-- 1
select x;
var xs2 = hashtable.Select(x => x); // <-- 2
错误1:找不到源类型'HashTable'的查询模式的实现。 '选择'未找到。考虑明确指定范围变量'x'的类型。
错误2:'HashTable'没有包含'Select'的定义,也没有找到接受类型'HashTable'的第一个参数的扩展方法'Select'(你缺少using指令还是程序集引用? )
也许有一些接口或继承技巧我不知道?
对于那些谁问,这里是接口的全树:
using SCG = System.Collections.Generic;
public class HashTable<TKey, TValue>
: IKeyedCollection<TKey, TValue>, SCG.IDictionary<TKey, TValue>
public interface IKeyedCollection<out TKey, out TValue>
: ICollection<IAssociation<TKey, TValue>>
public interface ICollection<out T> : SCG.IEnumerable<T>
public interface IAssociation<out TKey, out TValue>
// .NET Framework:
public interface IDictionary<TKey, TValue>
: ICollection<KeyValuePair<TKey, TValue>>
public interface ICollection<T>
: IEnumerable<T>
现在你可以看到,为什么我不能让KeyValuePair<TKey, TValue>
和IAssociation<TKey, TValue>
相同。
您是否尝试过手动指定'Select'的[generic arguments](http://msdn.microsoft.com/zh-CN/library/bb548891.aspx)?听起来像是无法推断它们。特别是,你需要手动指定'TSource',但是一旦你做了一个,你就需要做我想的。不幸的是,这会阻止你返回匿名类型而没有一些烦人的解决方法。 – 2013-02-14 15:39:29
您可以包含'HashTable'的继承/接口的其余部分吗? –
2013-02-14 15:40:01
那么它告诉你该怎么做,至少在第一个错误。您必须明确说明'x'是什么类型:from IAssociation x in hashtable select x; –
Stonehead
2013-02-14 15:41:04