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?
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?
if less .net 3.5
compareJobList .Exists(t => t.item1 == 3&& t.item2=1)
or
compareJobList .FindAll(t => t.item1 == 3&& t.item2=1)
compareJobList .Exists(t => t.item1 == 3&& t.item2=1)
or
compareJobList .FindAll(t => t.item1 == 3&& t.item2=1)
If you're using .NET 3.5 or later, LINQ is the answer to this one:I'd suggest Any instead of Where.
compareJobList .Where(t => t.item1 == 3);
using System.Linq;
...
bool exists = compareJobList .Any(t => t.item1 == 3);
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
A hashset will never store a duplicate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
compareJobList .Where(t => t.item1 == 3);