Link to home
Start Free TrialLog in
Avatar of jmpatton
jmpatton

asked on

Help finding an item within a list

I have a list of objects and before I add a new object to the list I need to see if it was already added.  I tried List.Items.Contains(), but it dodnt work.  Any help would be appreciated
CycleVolumeList cycleVolumeList = new CycleVolumeList();
cycleVolume = serviceClient.GetCycleVolume(Param1, Param2, Param3);

// Add it to cycleVolumeList
CycleVolume cycleVolume = new CycleVolume(item1, item2, cycleVolume );

                            if (cycleVolumeList.Items.Contains(new CycleVolume(item1, item2, cycleVolume)))
                            {
                              //do nothing  
                            }
                            else
                            {
                                cycleVolumeList.Items.Add(cycleVolume);
                            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dimaj
dimaj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmpatton
jmpatton

ASKER

Thanks for the quick reply.  Much apprieciated!
For Contains to work as you expect, CycleVolume must implement System.IEquatable<T>.

IEquatable<T> Interface (System)
http://msdn.microsoft.com/en-us/library/ms131187.aspx

I hope this helps.