0
我有一个绑定到某个列表。
比方说,我有一个当前的指数。现在,我从列表中删除几个项目(可能彼此相邻,也可能不相邻)。如果我想将当前索引重置为删除后的下一个项目(或者如果没有下一个项目,那么最后一个项目,假设还有剩下的项目),那么最好的方法是什么也没有做到这一点很多枚举。
基本上,我坚持的是我似乎需要在执行删除操作并在某处引用新对象之前弄清楚这一点,但似乎无法通过列举几个列表并引发我的困扰应用。
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做到这一点,但我努力寻找它。有任何想法吗?
谢谢。
Linq和Indices不是朋友。 –
好的。我想我明白了。我只使用Enumerator和NextItem(或者其他所谓的)。 – William