我需要优化我的代码。我有一些重复的代码。但我想优化它。任何人都可以帮助我优化我的代码。我怎样才能使共同功能为此?避免Linq在C#中重复组
foreach (var item in hotellocation.GroupBy(x => x).ToDictionary(g => g.Key, g => 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 => x).ToDictionary(g => g.Key, g => g.Count()))
{
if (item.Key != "")
{
lstHotelType.Add(new HotelTypeFilter()
{
Name = item.Key,
count = item.Value
});
}
}
我会摆脱的第一件事是ToDictionary - 它毫无意义,因为您从未将它用作字典。 – Jamiec 2014-09-19 10:01:37