title: Reverse FIFO qty item?
body: <p>following my 1st post already solved
  <a href="https://stackoverflow.com/questions/45706332/first-in-first-out-subtract-quantity-stock">First in first out subtract quantity stock?</a></p>
  
  <p>i have a database detail name <code>Item_detail</code></p>
  
  <pre><code>No(autoincrement)      Id_Item    qtyLimit     qtyReal      
  -------------------------------------------------------
         1                  1          30           0
         2                  1          40           0
         3                  2          50           0
         4                  2          60          10
  </code></pre>
  
  <p>so for item 1  sum all qty Real for item 1 is 0 , and qty limit is limit qty for each data in detail item</p>
  
  <p>if i have data from datagridview1 from <code>in_transaction</code></p>
  
  <pre><code>   Id_Item    qtyIn
     ---------------
        1         50        
        2         80
  </code></pre>
  
  <p>how to add quantity from my transaction into my <code>update Item_detail</code> but <strong>reverse</strong> from bottom to upper
  the result i want like this</p>
  
  <pre><code> No(autoincrement)     Id_Item      qtyLimit     qtyReal
  -------------------------------------------------------
         1                  1             30         10            
         2                  1             40         40           
         3                  2             50         30           
         4                  2             60         60           
  </code></pre>
  
  <p>so its checking limit id no 1 if qty limit = qty real the move from bottom to upper with same no id then check again with qty limit</p>
  
  <p>after that i wanna put that transaction process into table <code>Item_transaction</code></p>
  
  <pre><code> NoTrans(autoincrement) no(from item_detail)      qtyIn
  ---------------------------------------------------------
         1                       1                    10
         2                       2                    40
         3                       3                    30
         4                       4                    50
  </code></pre>
  
  <p>trycode</p>
  
  <pre><code>private void calculate()
  {
  // Transaction: Id_Item, qtyIn
  IEnumerable&lt;in_transaction&gt; contextItemIn = null;
  // ItemDetail: No, Id_Item, qtyLimit, qtyReal
  IEnumerable&lt;ItemDetail&gt; contextItemDetails = null;
  
  Dictionary&lt;int, decimal&gt; itemInQuantities = contextItemIn.ToDictionary(x =&gt; x.Id_Item, x =&gt; x.qtyIn);
  
  var itemDetails = contextItemDetails.OrderBy(x =&gt; x.No).ToList();
  
  foreach (var item in itemDetails.AsEnumerable().Reverse())
  {
      decimal inQty;
      if (itemInQuantities.TryGetValue(item.Id_Item, out inQty))
      {
          var qtyChange = inQty
          item.qtyReal += qtyChange;
          inQty -= qtyChange;
          if (inQty == item.QtyLimit)
          {
  
              itemInQuantities.Remove(item.Id_Item);
          }
          else
          {
              itemInQuantities[item.Id_Item] = inQty;
          }
          if (qtyChange != 0)
          {
              transactions.Add(new ItemTransaction { /*No = autoincrement, */ 
              NoTrans = item.No, qtyIn = qtyChange });
          }
      }
  }
  
  // itemDetails qty values are corrected
  </code></pre>
  
  <p>}</p>
  
  <p>so please help</p>