private void fix(bool deleteDuplcates) { //Step 1: Swap listenedTimes table ids of deleted media files with the selected id MediaPlayerInfoUserControl goodFileToStay = getMediaPlayerInfoUserControl(lbxMediaUnits.SelectedItem); if (goodFileToStay == null) { MessageBox.Show("No good file selected or smth..."); return; } var duplicatesToDeleteIDs = new List <long>(); var duplicatesToDeleteCtrls = new List <MediaPlayerInfoUserControl>(); foreach (object lbxItem in lbxMediaUnits.Items) { MediaPlayerInfoUserControl mib = getMediaPlayerInfoUserControl(lbxItem); var l = mib.MediaUnitID; if (l != goodFileToStay.MediaUnitID) { duplicatesToDeleteIDs.Add(l); duplicatesToDeleteCtrls.Add(mib); } } _mediaInfoDbSrc.SmartCascadingDeleteOfAll(duplicatesToDeleteIDs, goodFileToStay.MediaUnitID); //Step 2: File.Delete the rest of the files if (deleteDuplcates == true) { foreach (MediaPlayerInfoUserControl mib in duplicatesToDeleteCtrls) { if (File.Exists(mib.PathFileName)) { mib.DeleteFS(mib.PathFileName); } } } //Step 3: Reload the list int idx = lbxMediaUnits.SelectedIndex; onFindMatches(null, null); lbxMediaUnits.SelectedIndex = idx; tbxFilterAnd.Focus(); }