title: LINQ to dictionary with multiple keys for a single value
body: <p>Consider the following code:</p>
  
  <pre><code>public KeyAttribute : Attribute{
      public string Value;
      public KeyAttribute(string value){
          Value = value;
      }
  }
  
  [Key("A")]
  [Key("AB")]
  public class A : IClass
  {
      public string FieldA {get;set;}
  }
  
  [Key("B")]
  public class B : IClass
  {
      public string FieldB {get;set;}
  }
  </code></pre>
  
  <p>So I have to through all the implementations of <code>IClass</code> interface, and I've got to construct a dictionary <code>Dictionary&lt;string, ConstructorInfo&gt;</code> where the keys are the <code>Value</code> properties of <code>KeyAttribute</code>, and the values are the corresponding <code>ConstructorInfo</code>.<br>
  Notice that there might be several <code>KeyAttribute</code> on a single class, so for that case, there should be the corresponding amount of entries in the dictionary.</p>
  
  <p>For the current example, the desired outcome is:</p>
  
  <pre><code>key     | value
  --------+-------------------
  "A"     | ConstructorInfo A
  "AB"    | ConstructorInfo A
  "B"     | ConstructorInfo B
  </code></pre>
  
  <p>At first I wrote this:  </p>
  
  <pre><code>return AppDomain.CurrentDomain.GetAssemblies()
      .SelectMany(s =&gt; s.GetTypes())
      .Where(t =&gt; typeof(IClass).IsAssignableFrom(t))
      .ToDictionary(t =&gt;
      {
          var key = (KeyAttribute) t.GetCustomAttributes(typeof(KeyAttribute), true).First();
          return key.Value;
      }, t =&gt; t.GetConstructors(BindingFlags.Public).First());
  </code></pre>
  
  <p>But as you can see, the code above does not handle the situation with several attributes.
  So I did the following, but it's obviously wrong and I'm not sure how to correct it.</p>
  
  <pre><code>return AppDomain.CurrentDomain.GetAssemblies()
      .SelectMany(s =&gt; s.GetTypes())
      .Where(t =&gt; typeof(IClass).IsAssignableFrom(t))
      .ToDictionary(t =&gt;
      {
          return t.GetCustomAttributes(typeof(KeyAttribute), true).Select(a =&gt; ((KeyAttribute)a).Value);
      }, t =&gt; t.GetConstructors(BindingFlags.Public).First());
  </code></pre>
  
  <p>I know I can do that without LINQ like that:</p>
  
  <pre><code>var types = AppDomain.CurrentDomain.GetAssemblies()
      .SelectMany(s =&gt; s.GetTypes())
      .Where(t =&gt; typeof(IClass).IsAssignableFrom(t));
  var dict = new Dictionary&lt;string, ConstructorInfo&gt;();
  foreach (var type in types)
  {
      var keys = type.GetCustomAttributes(typeof(KeyAttribute), true).Cast&lt;KeyAttribute&gt;().Select(a =&gt; a.Value);
      var ctorInfo = type.GetConstructors(BindingFlags.Public).First();
      foreach (var key in keys)
      {
          dict.Add(key, ctorInfo);
      }
  }
  return dict;
  </code></pre>
  
  <p>But I'd rather stick to LINQ, if it is possible.</p>
  
  <p>Sorry about all these somewhat misleading details about attributes and all that, while it is a question about LINQ, but I couldn't think of any other vivid example.</p>