title: Summation of SortedDictionary in C# on the basis of specified intervals of keys
body: <p><a href="https://i.stack.imgur.com/a8tl5.png" rel="nofollow noreferrer">This is how initially value looks</a></p>
  <p><a href="https://i.stack.imgur.com/KeSAi.png" rel="nofollow noreferrer">After doing the sum of values based on specified interval, in this case, it summed values for keys in an interval of 0.5</a></p>
  <p>Currently, it is being done with the help of foreach on SortedDictionary but is there any other efficient way to do it, like LINQ? This will really help to optimize the code for performance issues.</p>
  <p>Current code implementation:</p>
  <pre><code>private NewMarketPrice[] ConsolidateBids(double tickSize, IEnumerable&lt;NewMarketPrice&gt; bids)
      {
          SortedDictionary&lt;double, int&gt; consolidatedBids = 
              new SortedDictionary&lt;double, int&gt;(new HighestPriceComparer());
  
          double lowestBid = Double.MaxValue;
          foreach(NewMarketPrice bid in bids)
          {
              if(bid.Price &lt; lowestBid)
              {
                  lowestBid = bid.Price;
              }
  
              double consolidatedPrice = RoundDownToNearestInterval(bid.Price, tickSize);
  
              int consolidatedQuantity;
              if(!consolidatedBids.TryGetValue(consolidatedPrice, out consolidatedQuantity))
              {
                  consolidatedQuantity = 0;
                  consolidatedBids.Add(consolidatedPrice, 0);
              }
  
              consolidatedQuantity += bid.Qty;
              consolidatedBids[consolidatedPrice] = consolidatedQuantity;
          }
  </code></pre>