title: Create Extension Method TryGetValue(TKey, out TValue) of Enumerable<KeyValuePair<TKey, TValue>> just like of Dictionary<TKey, TValue>
body: <p>I have a <code>Enumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;</code>. I want to create a <code>bool TryGetValue(TKey, out TValue)</code> extension method of it just like it is available in <code>Dictionary&lt;TKey, TValue&gt;</code>.</p>
  
  <p>I tried </p>
  
  <pre><code>public static bool TryGetValue&lt;TKey, TValue&gt;
  (this Enumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; mapping, TKey key, out TValue value)
  {
      bool retVal = false;
  
      KeyValuePair&lt;TKey, TValue&gt; kvp;
  
      kvp = mapping.First(x =&gt; x.Key.Equals(key));
  
      if(kvp.Key == null &amp;&amp; kvp.Value == null)
      {
          retVal = false;
          value = default(TValue);
      }
      else
      {
          retVal = true;
          value = kvp.Value;
      }
  
      return retval;
  }
  </code></pre>
  
  <p>Is this correct way? If not please suggest one.</p>
  
  <p><strong>Note:</strong> </p>
  
  <p>I cannot use a Dictionary because Keys are repeated. Moreover it will only return the first matching value? </p>
  
  <p><code>What happens to the rest?</code> </p>
  
  <p>We can leave them. I am sending KeyValuePair created from a DataTable. I am creating that DataTable using <code>order by columnname</code> in its query. </p>