11
public struct PLU
{ 
    public int ID { get; set; } 
    public string name { get; set; } 
    public double price { get; set; } 
    public int quantity {get;set;}
}

public static ObservableCollection<PLU> PLUList = new ObservableCollection<PLU>();

I have the ObservableCollection as above. Now I want to search the ID in the PLUList and get its index like this:

int index = PLUList.indexOf();
if (index > -1)
{
    // Do something here
}
else
{
    // Do sth else here..
}

What's the quick fix?

EDIT:

Let's assume that some items were added to PLUList and I want to add another new item. But before adding I want to check if the ID already exists in the list. If it does then I would like to add +1 to the quantity.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
user995387
  • 355
  • 3
  • 8
  • 17

5 Answers5

25

Use LINQ :-)

var q =  PLUList.Where(X => X.ID == 13).FirstOrDefault();
if(q != null) 
{
   // do stuff
}
else 
{
   // do other stuff
}

Use this, if you want to keep it a struct:

var q =  PLUList.IndexOf( PLUList.Where(X => X.ID == 13).FirstOrDefault() );
if(q > -1) 
{
   // do stuff
}
else 
{
   // do other stuff
}
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
3

Although this post is old and already answered, it can still be helpful to others so I here is my answer.

You can create extension methods similar to List<T>.FindIndex(...) methods:

public static class ObservableCollectionExtensions
{
    public static int FindIndex<T>(this ObservableCollection<T> ts, Predicate<T> match)
    {
        return ts.FindIndex(0, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, Predicate<T> match)
    {
        return ts.FindIndex(startIndex, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, int count, Predicate<T> match)
    {
        if (startIndex < 0) startIndex = 0;
        if (count > ts.Count) count = ts.Count;

        for (int i = startIndex; i < count; i++)
        {
            if (match(ts[i])) return i;
        }

        return -1;
    }
}

Usage:

int index = PLUList.FindIndex(x => x.ID == 13);
if (index > -1)
{
    // Do something here...
}
else
{
    // Do something else here...
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • `ObservableCollection`s tend to change asynchronously, these extensions are not thread-safe. If threading is not a concern, this answer performs better than the accepted answer – Sten Petrov Feb 16 '20 at 17:51
  • Any other way of finding the index is equally not thread safe, it is up to the programmer to take care of thread safety while searching for the index. – Eliahu Aaron Feb 16 '20 at 17:56
  • yes, but the thread-safety should be considered in your code. `col.FindIndex(match)` itself can break – Sten Petrov Feb 17 '20 at 08:12
3

If you want to retrieve the item from your list, just use LINQ:

PLU item = PLUList.Where(z => z.ID == 12).FirstOrDefault();

But this will return the item itself, not its index. Why do you want the index?

Also, you should use class instead of struct if possible. Then you could test item against null to see if the ID was found in the collection.

if (item != null)
{
    // Then the item was found
}
else
{
    // No item found !
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
ken2k
  • 48,145
  • 10
  • 116
  • 176
2

Here is a quick fix.

int findID = 3;
int foundID=  -1;
for (int i = 0; i< PLUList.Count; i++)
{
  if (PLUList[i].ID == findID)
  {
    foundID = i;
    break;
  }
}

// Your code.
if (foundID > -1) {
// Do something here
...
A.R.
  • 15,405
  • 19
  • 77
  • 123
  • There are several issues with this answer: 1) `<=` in the loop guard should be `<` as when the loop will come to the last iteration you will get a index out of bounds exception and 2) Need to be checking `PLUList[i].ID == findID`. – Strelok Feb 17 '12 at 13:07
  • @Strelok: See, this is why intellisense is evil! I can't just type into a textbox anymore =). Anyway, thanks for the tip. – A.R. Feb 17 '12 at 13:11
  • 1
    @Strelok: My real mistake was rushing to post the trivial answer! – A.R. Feb 17 '12 at 13:27
1

It's just a normal collection. You can just iterate over it, check the ID and return the index of the object.

int index = -1;

for(int i=0;i<PLUList.Count;i++) {
 PLU plu = PLUList[i];
 if (plu.ID == yourId) {
   index = i;
   break;
 }
}

if (index > -1) {
// Do something here
}
else {
// Do sth else here..
}

LINQ VERSION:

private void getIndexForID(PLUListint idToFind,ObservableCollection<PLU> PLUList) {
   PLU target = PLUList.Where( z => z.ID == yourID ).FirstOrDefault();
   return target == null ? -1 : PLUList.IndexOf ( target );
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Strelok
  • 50,229
  • 9
  • 102
  • 115