0

全て、

何かにバインドされたリストがあります。

現在のインデックス i があるとします。ここで、リストからいくつかのアイテムを削除します (隣り合っている場合とそうでない場合があります)。現在のインデックスを削除後の次のアイテムにリセットしたい場合 (または、次のアイテムがない場合は、アイテムが残っていると仮定して最後のアイテム)、あまりにもせずにこれを達成するための最良の方法は何でしょうか?多くの列挙。

基本的に、私が立ち往生しているのは、削除を実行して新しいオブジェクトをどこかで参照する前にこれを理解する必要があるようですが、いくつかのリストを列挙してアプリケーションを動かさなければこれを行うことができないようです.

List<Object> MyCoolList;
List<Object> ItemsIWillBeDeleting;
Object CurrentItem;

//For simplicity, assume all of these are set and known for the following code
int i = MyCoolList.IndexOf(CurrentItem);
Object NewCurrentItem = null;
if (MyCoolList.Any(a => MyCoolList.IndexOf(a) > i && !ItemsIWillBeDeleting.Any(b => b==a)))
{
    NewCurrentItem = MyCoolList.First(a => MyCoolList.IndexOf(a) > i && !ItemsIWillBeDeleting.Any(b => b==a));
    ItemsIWillBeDeleting.ForEach(a => MyCoolList.Remove(a));
    CurrentItem = NewCurrentItem;
}
else (if MyCoolList.Count > MyCoolList.Count)
{
    NewCurrentItem = MyCoolList.Last(a => !ItemsIWillBeDeleting.Any(b => b==a))
    ItemsIWillBeDeleting.ForEach(a => MyCoolList.Remove(a));
    CurrentItem = MyCoolList.Last();
}
else
{
    MyCoolList.Clear(); //Everything is in MyCoolList is also in ItemsIWillBeDeleting
    CurrentItem = null;
}

Linq でこれを行うためのより良い方法があると確信していますが、それを見つけるのに苦労しています。何か案は?

ありがとう。

4

1 に答える 1