Solved

List Class

Posted on 2012-12-20
5
352 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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