title: Aggregate duplicate records with Linq in polymorphic case
body: <p>I would like to aggregate records stored in a <code>List&lt;&gt;</code>. </p>
  
  <p>In the case I got a <code>List&lt;int&gt;</code>, the solution would be </p>
  
  <pre><code>var results = list.GroupBy(x =&gt; x).Select(g =&gt; g.Sum());
  </code></pre>
  
  <p>In the case I got a <code>List&lt;MyObject&gt;</code> with </p>
  
  <pre><code>public class MyObject
  {
     public MyObject(int pvalue)
     {
        Value = pvalue;
     }
  
     public int Value {get; set;}
  
     public override bool Equals(object obj)
     {
         var p = obj as MyObject;
         if (p == null)
            return false;
  
         return Value.Equals(p.Value);
     }
  
     public bool Equals(MyObject p)
     {
       if (p == null)
          return false;
  
       return Value.Equals(p.Value)
     }
  
     public override int GetHashCode()
     {
       int hash = 13;
       hash = (hash * 7) + Value.GetHashCode();
       return hash;
     }
   }
  </code></pre>
  
  <p>Then a solution would be :
  <code>myOjectList.GroupBy(x =&gt; x.Value).Select(g =&gt; new MyObject{ Value = g.Sum()});</code></p>
  
  <p>Now in the case I got a <code>List&lt;IMyObject&gt;</code>, <code>IMyObject</code> being an interface (or an abstract class), and its concrete implementations got specific properties (example classes below), how can I solve the polymorphism in the <code>Select</code> of the previous Linq statement ?</p>
  
  <pre><code>public interface IMyObject
  {
     int Value {get; set;}
     bool Equals(object obj);
     int GetHashCode();
  }
  
  public class MyObject1 : IMyObject
  {
     public MyObject1(int pvalue, string pname)
     {
        Value = pvalue;
        Name = pname;
     }
  
     public int Value {get; set;}
     public string Name { get; set; }
  
     public override bool Equals(object obj)
     {
         var p = obj as MyObject1;
         if (p == null)
            return false;
  
         return Name.Equals(p.Name) &amp;&amp; Value.Equals(p.Value);
    }
  
    public bool Equals(MyObject1 p)
    {
       if (p == null)
          return false;
  
       return Name.Equals(p.Name) &amp;&amp; Value.Equals(p.Value)
    }
  
    public override int GetHashCode()
    {
       int hash = 13;
       hash += (hash * 7) + Value.GetHashCode();
       hash += (hash * 7) + Name.GetHashCode();
       return hash;
    }
  }
  
  public class MyObject2 : IMyObject
  {
     public MyObject2(int pvalue, int pvalue2)
     {
        Value = pvalue;
        Value2 = pvalue2;
     }
  
     public int Value {get; set;}
     public string Value2 { get; set; }
  
     public override bool Equals(object obj)
     {
         var p = obj as MyObject1;
         if (p == null)
            return false;
  
         return Value2.Equals(p.Value2) &amp;&amp; Value.Equals(p.Value);
    }
  
    public bool Equals(MyObject1 p)
    {
       if (p == null)
          return false;
  
       return Value2.Equals(p.Value2) &amp;&amp; Value.Equals(p.Value)
    }
  
    public override int GetHashCode()
    {
       int hash = 13;
       hash += (hash * 7) + Value.GetHashCode();
       hash += (hash * 7) + Value2.GetHashCode();
       return hash;
    }
  }
  </code></pre>