Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have BidList (ObservableCollection) and I want to find index of the row in which for example Id==25.

How to do that?

What I have tried:

Tried with FindIndex and it doesn't work in my case
Posted
Updated 23-Jun-19 15:55pm

Try something like this:
C#
var itemindex =  BidList.IndexOf(BidList.Where(x=>x.Id==25).FirstOrDefault());
 
Share this answer
 
Comments
Sinisa Janjetovic 23-Jun-19 10:42am    
Thank you, but I am getting -1, and definitely it is not. I tested it with Id==20, and I should get 19.

Or I put this code on wrong place? I pasted it in part of the code I wrote in my question.

Should I keep symbol x as you wrote or I should replace it with something?
Maciej Los 23-Jun-19 11:12am    
I have no idea why you're getting -1.
Yes, you can replace [x] with any symbol or name, for example: Where(item=>item.Id==20)
Sinisa Janjetovic 23-Jun-19 11:35am    
Thank you, it works great. I've found one internal mistake, everything is proper.
Maciej Los 23-Jun-19 11:53am    
Great. Can you accept my answer as a solution?
Sinisa Janjetovic 23-Jun-19 18:48pm    
Yes, of course, you helped me a lot
One of the many powerful features of 'ObservableCollection<T> is the "state" information the 'CollectionChanged EventHandler gives you. By keeping track of the last change data, you can often avoid unnecessary code, and/or enable an 'undo feature:
bool suppressOCChangeAction = false;
bool keepOCLastChangeData = true;

NotifyCollectionChangedAction lastAction;
int prevNdx;
int newNdx;
IList oldItems;
IList newItems;

private void OCOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (suppressOCChangeAction) return;

    if (keepOCLastChangeData)
    {
        lastAction = e.Action;
        prevNdx = e.OldStartingIndex;
        newNdx = e.NewStartingIndex;
        oldItems = e.OldItems;
        newItems = e.NewItems;
    }

    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Add:
            break;
        case NotifyCollectionChangedAction.Remove:
            break;
        case NotifyCollectionChangedAction.Replace:
            break;
        case NotifyCollectionChangedAction.Move:
            break;
        case NotifyCollectionChangedAction.Reset:
            break;
    }
}
 
Share this answer
 
Comments
Maciej Los 24-Jun-19 4:04am    
5ed!
Sinisa Janjetovic 24-Jun-19 5:47am    
I am a little bit confused with this answer. Is it related to this question?
BillWoodruff 24-Jun-19 5:54am    
I added this because I thought it might be useful, and help avoid some unnecessary computation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900