这可能很简单,但我一直想不出一个解决方案。
我有一个:
ObservableCollection<ProcessModel> _collection = new ObservableCollection<ProcessModel>();这个集合填充了许多ProcessModel。
我的问题是,我有一个ProcessModel,我想在我的_collection中找到它。
我想这样做,这样我就能够找到ProcessModel在_collection中的位置的索引,我真的不确定如何做到这一点。
我之所以要这样做,是因为我想在ObservableCollection (_collection)中领先于它的ProcessModel N+1。
发布于 2012-05-16 20:26:03
var x = _collection[(_collection.IndexOf(ProcessItem) + 1)];发布于 2012-05-16 20:20:26
http://msdn.microsoft.com/en-us/library/ms132410.aspx
使用:
_collection.IndexOf(_item)下面是获取下一项的一些代码:
int nextIndex = _collection.IndexOf(_item) + 1;
if (nextIndex == 0)
{
// not found, you may want to handle this as a special case.
}
else if (nextIndex < _collection.Count)
{
_next = _collection[nextIndex];
}
else
{
// that was the last one
}发布于 2012-05-16 20:21:35
由于ObservableCollection是一个序列,因此我们可以使用LINQ
int index =
_collection.Select((x,i) => object.Equals(x, mydesiredProcessModel)? i + 1 : -1)
.Where(x => x != -1).FirstOrDefault();
ProcessModel pm = _collection.ElementAt(index);我已经将你的索引增加到1,它符合你的要求。
或
ProcessModel pm = _collection[_collection.IndexOf(mydesiredProcessModel) + 1];或
ProcessModel pm = _collection.ElementAt(_collection.IndexOf(mydesiredProcessModel) + 1);非空的编辑
int i = _collection.IndexOf(ProcessItem) + 1;
var x;
if (i <= _collection.Count - 1) // Index start from 0 to LengthofCollection - 1
x = _collection[i];
else
MessageBox.Show("Item does not exist");https://stackoverflow.com/questions/10618351
复制相似问题