Note that there are some explanatory texts on larger screens.

plurals
  1. POSingle extension method on IDictionary<K, IEnumerable/IList/ICollection<V>>
    primarykey
    data
    text
    <p>I'm trying to write an extension method that will convert <code>IDictionary&lt;K, S&lt;V&gt;&gt;</code> holding any type of collection/sequence (<code>S&lt;V&gt;</code>) to <code>ILookup&lt;K, V&gt;</code> which is more proper data structure in those cases. This means I'd like my extension to work on different <code>S</code> types and interfaces:</p> <ul> <li><code>IDictionary&lt;K, IEnumerable&lt;V&gt;&gt;</code></li> <li><code>IDictionary&lt;K, ICollection&lt;V&gt;&gt;</code></li> <li><code>IDictionary&lt;K, List&lt;V&gt;&gt;</code></li> </ul> <p>etc. Ideally, I don't want to write separate implementation for each possible collection type AND I want type inference to do its job.</p> <p>What I've tried is:</p> <pre><code>public static ILookup&lt;TKey, TValue&gt;ToLookup&lt;TKey, TCollection, TValue&gt;( this IDictionary&lt;TKey, TCollection&gt; dictionary) where TCollection : IEnumerable&lt;TValue&gt; </code></pre> <p>But it have no <code>TValue</code> in parameters list, so type inference is unable to figure it out - I get "The type arguments for method ToLookup cannot be inferred from the usage".</p> <p>Is there a chance it could work somehow in other way than adding fake <code>TValue</code>-typed parameter to the method?</p> <p><strong>Examples of expected usage</strong></p> <p>I hope all above calls to be possible and result in a call to my single extension method:</p> <pre><code>var dictOfIEnumerables = new Dictionary&lt;int, IEnumerable&lt;int&gt;&gt;(); var lookupFromIEnumerables = dictOfIEnumerables.ToLookup(); var dictOfICollections = new Dictionary&lt;int, ICollection&lt;int&gt;&gt;(); var lookupFromICollections = dictOfICollections.ToLookup(); var dictOfLists = new Dictionary&lt;int, List&lt;int&gt;&gt;(); var lookupFromLists = dictOfLists.ToLookup(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload