title: Pivot in C# with Linq
body: <p>I have the following <code>LINQ</code> grouping clause. Where I want money divided into months. And it works but the money (Deckungsbeitrag - German format: <code>##,##</code>) is always 0. Am I missing to change format or something? I already tried different conversions in the following script:</p>
  
  <pre><code>  var results = (from r1 in table.AsEnumerable() group r1 by new {
            Geschäftspartner = r1.Field &lt; string &gt; ("Geschäftspartner"),
                Kunde = r1.Field &lt; string &gt; ("Kunde_KZ"),
                //Monat = r1.Field&lt;string&gt;("Monat")
        }
        into g select new {
  
            Kunde_Kz = g.Key.Kunde,
                Geschäftspartner = g.Key.Geschäftspartner,
                Jan = g.Where(c =&gt; c.Field &lt; string &gt; ("Monat") == "7_2017").Sum(c =&gt; Convert.ToDouble((c.Field &lt; string &gt; ("Deckungsbeitrag").Replace(",", ".")))),
                Feb = g.Where(c =&gt; c.Field &lt; string &gt; ("Monat") == "8_2017").Sum(c =&gt; c.Field &lt; float &gt; ("Deckungsbeitrag")),
                March = g.Where(c =&gt; c.Field &lt; string &gt; ("Monat") == "9_2017").Sum(c =&gt; c.Field &lt; double &gt; ("Deckungsbeitrag"))
        }).ToList();
  </code></pre>
  
  <p><strong>Edit</strong>
  The input table is like  </p>
  
  <blockquote>
    <p>xyv|kut|23,45|7_2017|</p>
  </blockquote>
  
  <p>So there can be null values for certain months on certain customer(Kunden) for the Money, so i tried this: </p>
  
  <pre><code>Jan = g.Where(c =&gt; c.Field&lt;string&gt;("Monat").Contains("/7_2017/")).Sum(c =&gt; (double?)c.Field&lt;decimal&gt;("Deckungsbeitrag")) ?? 0.0,
      Feb = g.Where(c =&gt; c.Field&lt;string&gt;("Monat").Contains("/8_2017/")).Sum(c =&gt; (double?)c.Field&lt;decimal&gt;("Deckungsbeitrag")) ?? 0.0,
       March = g.Where(c =&gt; c.Field&lt;string&gt;("Monat").Contains("/9_2017/")).Sum(c =&gt; (double?)c.Field&lt;decimal&gt;("Deckungsbeitrag")) ?? 0.0
  </code></pre>
  
  <p>But still only 0 values for money...</p>
  
  <p>Output in the Grid:</p>
  
  <pre><code>Select KATHOPEGN2 "Katholische Kirchenstiftung ""Herz Jesu""" 0 0 0 
  Select IMGAECREU1 "Landgasthof ""In Gaertlein """ 0 0 0 
  Select 180GRGERA1 180 Grad-Event GmbH 0 0 0 
  Select RASSFBETZ1 1a autoservice Rass 0 0 0 
  Select ASDIEBAYR1 A. S. Dienstleistungen 0 0 0 
  Select VEOANAK a.n.a. Kreislaufwirtschaft 0 0 0 
  Select ABWASEGLO1 Abwasserzweckverband Trubachtal 0 0 0 
  Select OELMEPEGN1 Adolf Oelmeier 0 0 0 
  Select AECGMBADB1 AEC Alternative Energieconzepte GmbH 0 0 0 
  Select VLLASPEGN1 Afrim Vllasalija 0 0 0 
  Select AGIPSBAYR1 Agip Station Hoellrich 0 0 0 
  </code></pre>