从ILookup中选择元组会引发异常

时间:2018-10-25 17:01:22

标签: c# linq valuetuple

我有一个ILookup<Type, (int, string, BitmapSource)>,应该在一个下拉列表中存储元素的显示信息(否则该元素仅作为应用程序中的枚举存在)。

元组的访问方式如下:

public IEnumerable<(int, string, BitmapSource)> EnumerationValues(Type type)
{
  return this._enumerationValues
             .Where(group => group.Key == type)
             .Select(group => group.SelectMany<(int, string, BitmapSource),
                                               (int, string, BitmapSource)>(element => element));
}

但是,编译器对此抱怨:

  

无法将lambda表达式转换为预期的委托类型,因为该块中的某些返回类型不能隐式转换为委托返回类型。

即使写element => (element.Item1, element.Item2, element.Item3)也会导致相同的错误。我在这里做错什么,类型完全一样。

1 个答案:

答案 0 :(得分:2)

获取与给定键关联的值的方法是使用索引器。这是专门设计用来返回与该键关联的值序列的操作。试图在整个集合中搜索匹配的键会破坏首先要进行查找的目的,因为它是专门为快速搜索给定键而设计的数据结构。

public IEnumerable<(int, string, BitmapSource)> EnumerationValues(Type type) =>
    _enumerationValues[type];