title: C# Groupby then Sum after Split CSV (no Headers)
body: <p>Having read the various post, I think I'm close to getting the result I want but the &quot;sum&quot; part is giving the wrong answer.  I suspect its due to the way I call the &quot;sum&quot; as my csv file does not have column headers.</p>
  <p>csv file contains the following:</p>
  <blockquote>
  <p>222, 1</p>
  <p>223, 2</p>
  <p>222, 1</p>
  <p>224, 2</p>
  <p>222, -1</p>
  </blockquote>
  <p>What I want to is to:</p>
  <ul>
  <li>read the CSV file and split the data</li>
  <li>group by first column</li>
  <li>sum second column by group</li>
  </ul>
  <p>Here is what I have done so far:</p>
  <pre><code>var newquery = from line in File.ReadAllLines(path_source) //read all lines in csv file
                         let values = line.Split(',') //split the values by separator
                         let price_value = values[0]                                                      
                         group line by price_value into g                           
                         orderby g.Key
                         //select g;
                         select new 
                         {
                             price_column = g.Key,
                             qty_column = g.Sum(x =&gt; x[1]) 
                         };
  
  var writesum = newquery.Select(record =&gt; record.price_column + &quot;,&quot; + record.qty_column);
  
  File.WriteAllLines(path_target, writesum);
  </code></pre>
  <p>The path_target file shows:</p>
  <blockquote>
  <p>222, 45</p>
  <p>223, 80</p>
  <p>224, 65</p>
  </blockquote>
  <p>This tells me that the <code>split</code>, <code>groupby</code>, <code>orderby</code> syntax is correct but the <code>sum</code> is totally wrong.</p>
  <p>I suspect one of the reasons the results for the <code>sum</code> is wrong is due to the syntax here <code>qty_column = g.Sum(x =&gt; x[1])</code>.</p>
  <p>As my data does not contain headers, I'm not able to call <code>x.something</code> as in the examples.</p>
  <p>On the other hand I could be totally wrong on this point!</p>
  <p>Any help is greatly appreciated.</p>