Solved

List Class

Posted on 2012-12-20
5
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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