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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
IEquatable<T> Interface (System)
http://msdn.microsoft.com/en-us/library/ms131187.aspx
I hope this helps.
Why not just use a HashSet?
http://msdn.microsoft.com/en-us/library/bb359438.aspx
http://stackoverflow.com/questions/918742/efficient-list-of-unique-strings-c
-saige-
http://msdn.microsoft.com/en-us/library/bb359438.aspx
http://stackoverflow.com/questions/918742/efficient-list-of-unique-strings-c
-saige-
ASKER