title: Avoid Repeating Group by Linq in C#
body: <p>I need to optimize My Code. I Have Some Repeating Code. But I would like to optimize it. Can any one please help me to optimize My Code. How Can I greate Common Function For this???</p>
  
  <pre><code> foreach (var item in hotellocation.GroupBy(x =&gt; x).ToDictionary(g =&gt; g.Key, g =&gt; g.Count()))
              {
                  if (item.Key != "")
                  {
                      lstHotelLocation.Add(new HotelLocation()
                          {
                              Name = item.Key,
                              count = item.Value
                          });
                  }
              }
  
              //need to Apply to linq
  
              foreach (var item in hoteltype.GroupBy(x =&gt; x).ToDictionary(g =&gt; g.Key, g =&gt; g.Count()))
              {               
                  if (item.Key != "")
                  {
                      lstHotelType.Add(new HotelTypeFilter()
                      {
                          Name = item.Key,
                          count = item.Value
                      });
                  }
              }
  </code></pre>