Search code examples
c#linqvaluetuple

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.


Solution

  • 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];