Solved

List Class

Posted on 2012-12-20
5
344 Views
Last Modified: 2013-01-05
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?
0
Comment
Question by:andyw27
5 Comments
 
LVL 18

Expert Comment

by:dj_alik
ID: 38710377
If you're using .NET 3.5 or later, LINQ is the answer to this one:
compareJobList .Where(t => t.item1 == 3);
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 38710391
if  less .net 3.5
compareJobList .Exists(t => t.item1 == 3&& t.item2=1)
or
compareJobList .FindAll(t => t.item1 == 3&& t.item2=1)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38710483
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

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38711936
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
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 38712372
To be able to check if an item is in a collection, you need to be able to compare them. The default comparison for objects only compares the references, so if you have two objects with the exact same values, they will still not be equal.

Implement the IEquatable<T> interface for the class:

public class CompareItems : IEquatable<CompareItems> {
	public string type { get; set; }
	public int item1 { get; set; }
	public int item2 { get; set; }

	public bool Equals(CompareItems other) {
		return type == other.type && item1 == other.item1 && item2 == other.item2;
	}

}

Open in new window


Now you can check if an item exists in the list. In this example, only the first item will be added to the list:

CompareItems item1 = new CompareItems { type = "test", item1 = 42, item2 = 1337 };
if (!compareJobList.Contains(item1)) {
	compareJobList.Add(item1);
}

CompareItems item2 = new CompareItems { type = "test", item1 = 42, item2 = 1337 };
if (!compareJobList.Contains(item2)) {
	compareJobList.Add(item2);
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 40
vb.net - How to check if current user is an administrator? 6 34
Class object 2 26
Replace &lt; with < 14 54
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now