From b22da76efc1c119a8fe15888402fbe4f0746046e Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Thu, 7 Jun 2018 16:40:06 -0400 Subject: [PATCH] Improves IntersectWith code Take advantage of the Dictionnaries --- .../Configuration/Device/DeviceInfoCollection.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/SoundSwitch/Framework/Configuration/Device/DeviceInfoCollection.cs b/SoundSwitch/Framework/Configuration/Device/DeviceInfoCollection.cs index f6d297c495..61ffb3d897 100644 --- a/SoundSwitch/Framework/Configuration/Device/DeviceInfoCollection.cs +++ b/SoundSwitch/Framework/Configuration/Device/DeviceInfoCollection.cs @@ -30,13 +30,21 @@ public DeviceInfoCollection(HashSet devices) /// public IEnumerable IntersectWith(IEnumerable devices) { - var result = devices.Join(_deviceById, device => device.ID, pair => pair.Key, (device, pair) => device); - if (result.GetEnumerator().Current != null) + var devicesResult = new Dictionary(); + foreach (var mmDevice in devices) { - return result; + if (devicesResult.ContainsKey(mmDevice.ID)) + continue; + + if (!_deviceById.ContainsKey(mmDevice.ID) && !_deviceByName.ContainsKey(mmDevice.FriendlyName)) + continue; + + devicesResult.Add(mmDevice.ID, mmDevice); + } - return devices.Join(_deviceByName, device => device.FriendlyName, pair => pair.Key, (device, pair) => device); + return devicesResult.Values; + } public IEnumerator GetEnumerator()