Solved

List Class

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

20 Experts available now in Live!

Get 1:1 Help Now