Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

List Class

Hi,

I have implemented a class that is a list:

Public class CompareItems
{
      Public string type {get; set; }
      Public int item1 { get; set; }
      Public int item2 { get; set; }
}

Public List< CompareItems> compareJobList = new List< CompareItems>();

I can populate this okay, but I now have a requirement to check that the list does not accept duplicate values.  I know this could be done on normal list by using the contains function.

However can it be done on this type of a list?

So I would need to check all 3 values don't exist?
Avatar of dj_alik
dj_alik

If you're using .NET 3.5 or later, LINQ is the answer to this one:
compareJobList .Where(t => t.item1 == 3);
if  less .net 3.5
compareJobList .Exists(t => t.item1 == 3&& t.item2=1)
or
compareJobList .FindAll(t => t.item1 == 3&& t.item2=1)
Avatar of kaufmed
If you're using .NET 3.5 or later, LINQ is the answer to this one:
compareJobList .Where(t => t.item1 == 3);
I'd suggest Any instead of Where.

using System.Linq;

...

bool exists = compareJobList .Any(t => t.item1 == 3);

Open in new window

Instead of a List<> you can use a HashSet<> (http://msdn.microsoft.com/en-us/library/bb359438.aspx)
A hashset will never store a duplicate
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden 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