title: using group by for getting some statistic data from one table with bulk records and puting the results into the second table
body: <p>As I explained in title I have 2 tables :
  1 - &quot;Leaves&quot; with 21000 rec of leaves for about 50 peoples during 20 years
  2- &quot;StatisticLeave&quot; which is empty
  now i want to use group by for getting some statistic data from table 1 and after doing some calculation (like sum &amp; ...) putting the results into the second table.
  I wrote below code :</p>
  <pre><code>public ActionResult UserStatistic()
      {
          
          var ST = new List&lt;StatisticLeave&gt;();
          var resuls = db.Leaves.GroupBy(p =&gt; p.Pcode);
          foreach (var Pcode in resuls)
          {
              var statistic = new StatisticLeave();
              foreach (var item in Pcode)
              { 
                  var used = UsedLeaves(item.Pcode);
                  var estelaji = db.Leaves.Where(p =&gt; p.DLT == Leave.DLType.Estelaji).Sum(p =&gt; p.LeaveDays);
                  var bh = db.Leaves.Where(p =&gt; p.DLT == Leave.DLType.Bihoghoogh).Sum(p =&gt; p.LeaveDays);               
                  statistic.Yearlyhours = ViewBag.mins/60;
                  statistic.YearlyDays = ViewBag.days;
                  statistic.YearEstelaji = estelaji;
                  statistic.YearBihoghoogh = bh;
                  statistic.Pcode = item.Pcode;
                  statistic.Year = item.HijriYear;
                  statistic.UsedYearLaeve = (used / 60) / 8;
                  ST.Add(statistic);
              }
              db.StatisticLeave.AddRange(ST);
          }
          db.SaveChanges();
          return View();
      }
  </code></pre>
  <p>when I trace the code i get the following caution:</p>
  <p>&quot;System.InvalidOperationException
  HResult=0x80131509
  Message=Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side.&quot;</p>
  <p>would you please tell me where is the problem or how can i fix it.</p>