From 276395e5f47165950aa609315afb33a251e76062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Tue, 9 Feb 2021 15:59:55 +0800 Subject: [PATCH] Use customized distinctBy instead of customized equalitycomparator --- .../Programs/Win32.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs b/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs index f2adbf243..d73483818 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs @@ -446,18 +446,13 @@ namespace Flow.Launcher.Plugin.Program.Programs } } - private class Win32ComparatorWithDescription : IEqualityComparer + public static IEnumerable DistinctBy(IEnumerable source, Func selector) { - public static readonly Win32ComparatorWithDescription Default = new Win32ComparatorWithDescription(); - - public bool Equals(Win32 x, Win32 y) + var set = new HashSet(); + 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(); }