Use customized distinctBy instead of customized equalitycomparator

This commit is contained in:
弘韬 张 2021-02-09 15:59:55 +08:00
parent a4160a14e8
commit 276395e5f4

View file

@ -446,18 +446,13 @@ namespace Flow.Launcher.Plugin.Program.Programs
}
}
private class Win32ComparatorWithDescription : IEqualityComparer<Win32>
public static IEnumerable<T> DistinctBy<T, R>(IEnumerable<T> source, Func<T,R> selector)
{
public static readonly Win32ComparatorWithDescription Default = new Win32ComparatorWithDescription();
public bool Equals(Win32 x, Win32 y)
var set = new HashSet<R>();
foreach (var item in source)
{
return x?.Description == y?.Description;
}
public int GetHashCode(Win32 obj)
{
return obj.Description.GetHashCode();
if (set.Add(selector(item)))
yield return item;
}
}
@ -467,8 +462,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
.SelectMany(g =>
{
if (g.Count() > 1)
return g.Where(p => !string.IsNullOrEmpty(p.Description))
.Distinct(Win32ComparatorWithDescription.Default);
return DistinctBy(g.Where(p => !string.IsNullOrEmpty(p.Description)), x => x.Description);
return g;
}).ToArray();
}