title: How to Group by and sum in C#
body: <pre><code> var SCount = new List&lt;object&gt;();
  </code></pre>
  
  <h1>First time Add DataTable dt1</h1>
  
  <p>DataSet ds = s.SelectProvider(Session["sender"], Provider, YearMonth);</p>
  
  <pre><code>                DateTime dt = new DateTime();
                  if (ds.Tables[0].Rows.Count &gt; 0)
                  {
                      string code = string.Empty; 
  
                      foreach (DataRow r in ds.Tables[0].Rows)
                      {
                          dt = Convert.ToDateTime(r["CDate"]);                             
                          SCount.Add(new { Mydate = dt.ToString("%d"), Mycount = ((480 - Convert.ToInt32(r["Minutes"])) / 30).ToString() }); 
                      } 
                  }
  </code></pre>
  
  <h1>Second Time Add Through Object</h1>
  
  <pre><code>         var kar= kd.GetCalender4HomePage(Id);
                      foreach (var i in kar)
                      {
                          dt = Convert.ToDateTime(i.EventDate);
                          if(YearMonth == dt.ToString("yyyy-MM"))
                              SCount.Add(new { Mydate = dt.ToString("%d"), Mycount = ((480 - Convert.ToInt32(i.ValueOne)) / 30).ToString() });
                      }
  </code></pre>
  
  <p>Result Is</p>
  
  <pre><code>          Mydate               |   MyCount
       ===========================================
               12                    30
               12                    10
               13                    11
  </code></pre>
  
  <p>But I need Result like</p>
  
  <pre><code>          Mydate               |   MyCount
       ===========================================
               12                    40
               13                    11
  
  
      var result = SCount.GroupBy(o =&gt; o.Mydate).Select(o =&gt; new { Date = o.Key, MyCount = o.Sum(s =&gt; s.Mycount) });
  </code></pre>
  
  <p>I tried. Its Come Error
        object' does not contain a definition for 'Mydate' and no extension method 'Mydate' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)   </p>