c# - Selecting Tuples from ILookup throws exception
问题描述
I have an ILookup<Type, (int, string, BitmapSource)>
which is supposed to store display information for elements (that otherwise only exist as enums in the application) in a dropdown.
The Tuples are accessed like this:
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));
}
However, the compiler complains about this:
Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type.
Even writing element => (element.Item1, element.Item2, element.Item3)
causes the same error. What am I doing wrong here, the types are exactly the same.
解决方案
The way to get the values associated with a given key is to use the indexer. That's the operation that is specifically designed to return the sequence of values associated with that key. Trying to search through the entire collection for the matching key defeats the entire purpose of having a lookup in the first place, as it's a data structure specifically designed for quickly searching for a given key.
public IEnumerable<(int, string, BitmapSource)> EnumerationValues(Type type) =>
_enumerationValues[type];
推荐阅读
- javascript - Use array filter with both conditions
- c - 将表格写入文件(二进制文件)后无法显示
- swift - filePromiseProvider writePromiseTo URL not working when dragging image to another application
- python - 基于另一个数据帧在 for 循环中创建数据帧
- sql - 使用 IN() 函数计算变量
- arraylist - How to parcelize Google's Place class?
- java - 返回 可选
而不是可选的 这是里面 - flask - 如何在 sqlalchemy 的 before_insert 事件上添加模型的关系实例?
- objective-c - 在不解决依赖关系的情况下获取objective-C类的AST节点
- python - 如何在 holoviews + bokeh 中获得带有图例标签的全高垂直线?