Eri*_* J. 4 .net c# linq linq-to-entities
我有一个班级计数器,用钥匙计算东西.简化:
public class Counter<T> {
private Dictionary<T, int> counts;
public void Increment(T key) {
int current;
bool exists = counts.TryGetValue(key, out current);
if (exists) {
counts[key]++;
} else {
counts[key] = 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它还有许多其他专门针对我需求的东西,但这才是最重要的.到目前为止,它运作良好.
现在我想让它在Linq查询中使用(包含键和值).做到这一点,我想我需要实施
IEnumerable<T, int>
Run Code Online (Sandbox Code Playgroud)
所以我补充说:
public class Counter<T> : IEnumerable<KeyValuePair<T, int>> {
// ...
IEnumerator<KeyValuePair<T, int>>
IEnumerable<KeyValuePair<T, int>>.GetEnumerator()
{
return ((IEnumerable<KeyValuePair<T, int>>)counts).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return counts.GetEnumerator();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会导致编译器错误
提供的泛型参数的数量不等于泛型类型定义的arity.参数名称:实例化
问题
更新:错字
我有一个拼写错误,同时简化我的代码发布.代码实际上是试图实现IEnumerable<KeyValuePair<T, int>>而不是IEnumerable<T, int>
counts是你修改的字典,这是副作用.以下是按键获取项目计数的方法:
var counters = keyedData
.GroupBy(item => item.MyKey)
.ToDictionary(g => g.Key, g => g.Count());
Run Code Online (Sandbox Code Playgroud)