Index Search in ObservableCollection

It may be very simple, but I could not find a solution.

I have:

ObservableCollection<ProcessModel> _collection = new ObservableCollection<ProcessModel>();

This collection is populated by a variety of ProcessModel.

My question is that I have a ProcessModel that I want to find in my _collection.

I want to do this to find the index where the ProcessModel was in _collection, I'm really not sure how to do this.

I want to do this because I want to get in ProcessModel N + 1 in front of it in ObservableCollection (_collection).

+3
source share
3 answers
var x = _collection[(_collection.IndexOf(ProcessItem) + 1)];
+7
source

http://msdn.microsoft.com/en-us/library/ms132410.aspx

Using:

_collection.IndexOf(_item)

Here is some code to get the following element:

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
}
+4
source

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);

EDIT for Not Null

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");
+3
source

All Articles