C# 避免在C中重复按Linq分组#
我需要优化我的代码。我有一些重复代码。但我想优化它。谁能帮我优化我的代码。我怎样才能把这个常用函数变大C# 避免在C中重复按Linq分组#,c#,asp.net-mvc,linq,optimization,C#,Asp.net Mvc,Linq,Optimization,我需要优化我的代码。我有一些重复代码。但我想优化它。谁能帮我优化我的代码。我怎样才能把这个常用函数变大 foreach (var item in hotellocation.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count())) { if (item.Key != "") { lstHote
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
});
}
}
首先要做的是去掉那些
foreach
循环,因为它们与LINQ不一致,并抛弃字典,因为它毫无意义:
var lstHotelLocation = hotellocation.GroupBy(x => x)
.Where(g => g.Key != "")
.Select(g => new HotelLocation {
Name = kv.Key,
count = g.Count()
})
.ToList();
var lstHotelType = hoteltype.GroupBy(x => x)
.Where(g => g.Key != "")
.Select(g => new HotelTypeFilter {
Name = g.Key,
count = g.Count()
})
.ToList();
如果要进一步删除副本,可以执行以下操作:
static List<T> AssembleCounts<T>(IEnumerable<string> values,
Func<string, int, T> makeObject)
{
return values.Where(x => !string.IsNullOrEmpty(x))
.GroupBy(x => x)
.Select(g => makeObject(g.Key, g.Count()))
.ToList();
}
var lstHotelLocation = AssembleCounts(hotellocation,
(k, c) => new HotelLocation {
Name = k, count = c
});
var lstHotelType = AssembleCounts(hoteltype,
(k, c) => new HotelTypeFilter {
Name = k, count = c
});
静态列表集合计数(IEnumerable值,
Func(对象)
{
返回值。其中(x=>!string.IsNullOrEmpty(x))
.GroupBy(x=>x)
.Select(g=>makeObject(g.Key,g.Count())
.ToList();
}
var lstHotelLocation=AssembleCounts(hotellocation,
(k,c)=>新酒店位置{
Name=k,count=c
});
var lstHotelType=AssembleCounts(hoteltype,
(k,c)=>新的HotelTypeFilter{
Name=k,count=c
});
首先要做的是去掉那些foreach
循环,因为它们与LINQ不一致,并且放弃字典,因为它是毫无意义的:
var lstHotelLocation = hotellocation.GroupBy(x => x)
.Where(g => g.Key != "")
.Select(g => new HotelLocation {
Name = kv.Key,
count = g.Count()
})
.ToList();
var lstHotelType = hoteltype.GroupBy(x => x)
.Where(g => g.Key != "")
.Select(g => new HotelTypeFilter {
Name = g.Key,
count = g.Count()
})
.ToList();
如果要进一步删除副本,可以执行以下操作:
static List<T> AssembleCounts<T>(IEnumerable<string> values,
Func<string, int, T> makeObject)
{
return values.Where(x => !string.IsNullOrEmpty(x))
.GroupBy(x => x)
.Select(g => makeObject(g.Key, g.Count()))
.ToList();
}
var lstHotelLocation = AssembleCounts(hotellocation,
(k, c) => new HotelLocation {
Name = k, count = c
});
var lstHotelType = AssembleCounts(hoteltype,
(k, c) => new HotelTypeFilter {
Name = k, count = c
});
静态列表集合计数(IEnumerable值,
Func(对象)
{
返回值。其中(x=>!string.IsNullOrEmpty(x))
.GroupBy(x=>x)
.Select(g=>makeObject(g.Key,g.Count())
.ToList();
}
var lstHotelLocation=AssembleCounts(hotellocation,
(k,c)=>新酒店位置{
Name=k,count=c
});
var lstHotelType=AssembleCounts(hoteltype,
(k,c)=>新的HotelTypeFilter{
Name=k,count=c
});
我要摆脱的第一件事是ToDictionary
-它毫无意义,因为你从来没有把它当作字典使用。我要摆脱的第一件事是ToDictionary
-它毫无意义,因为你从来没有把它当作字典使用。count=g.Value
需要更改为count=g.count()
(仅第二个示例)如果您已删除字典,则如果您已删除字典,则需要将count=g.Value
更改为count=g.count()
(仅第二个示例)