title: c# linq, sum distinct values by index
body: <p>I have multiple List with multiple values that may or may not have the same number of items..
  I need to add up only the numbers for a distinct name by index..</p>
  
  <p>heres some code to help explain...</p>
  
  <pre><code>    public static List&lt;p&gt; pList= new List&lt;p&gt;();
      public class p
      {
          public string name;
          public double sum;
          public List&lt;double&gt; ip;
      }
  </code></pre>
  
  <p>So, I have class "p", as a list..
  some of the names in class "p" will be same and some different..</p>
  
  <p>I need to sum the values in "ip" by index for only the class "p" that has the same name..</p>
  
  <p>Data Example..</p>
  
  <pre><code>name = a, sum = 10, ip = {2,2,2,2,2}    
  name = a, sum = 10, ip = {0,0,5,5}    
  name = a, sum = 5, ip = {0,0,5}    
  name = b, sum = 20, ip = {0,20}    
  name = b, sum = 10, ip = {10}    
  </code></pre>
  
  <p>Result..</p>
  
  <pre><code>name = a, sum = 25, ip = {2,2,12,7,2}    
  name = b, sum = 30, ip = {10,20}    
  </code></pre>
  
  <p>Below is the code I have already written..</p>
  
  <pre><code>List&lt;p&gt; distinctmerge = pList
                           .GroupBy(t =&gt; t.name)
                           .Select(g =&gt; new p
                           {
                               name = g.Select(a =&gt; a.name).FirstOrDefault(),
                               sum = g.Sum(a =&gt; a.sum),
                               ip = new List&lt;double&gt;() { 1, 2, 3 }
                           }).ToList();
  </code></pre>
  
  <p>The part I cant figure out is here..</p>
  
  <pre><code>ip = new List&lt;double&gt;() { 1, 2, 3 }
  </code></pre>