?
Solved

List<> distinct value

Posted on 2008-06-14
3
Medium Priority
?
10,711 Views
Last Modified: 2013-12-17
I have an object that I call MyObject.  Its parameters are MyObject.Id, MyObject.Name.. etc.
I create a List of this Object.
List<MyObject> myObjectList = new List<MyObject>();
I would like to add to myDistinctList all Objects from myObjectList, except where object exists with the same value in MyObject.Id in myDistinctList.
List<MyObject> myDistinctList = new List<MyObject>();
That is no 2 Objects in myDistinctList may contain the same value in MyObject.Id.
What would be the best way to do this?
0
Comment
Question by:johnkainn
[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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
gregmulvihill earned 600 total points
ID: 21786025
If I understand what you are looking for, this should work.  Though, I can't claim this is the 'best way' to achieve your goal, it does seem to be a simplest way that works.
using System;
using System.Collections.Generic;
using System.Text;
 
namespace EE_List_distinct_value
{
	class Program
	{
		private static List<MyObject> myObjectList = new List<MyObject>();
		private static List<MyObject> myDistinctList = new List<MyObject>();
 
		static void Main(string[] args)
		{
			myObjectList.Add(new MyObject("1", "Name_1"));
			myObjectList.Add(new MyObject("4", "Name_4"));
			myObjectList.Add(new MyObject("15", "Name_15"));
 
			AddUnique(myObjectList, myDistinctList);
 
			ShowMyObjects("myObjectList", myObjectList);
			ShowMyObjects("myDistinctList", myDistinctList);
 
			myObjectList.Add(new MyObject("11", "Name_11"));
			myObjectList.Add(new MyObject("15", "Name_15"));
			myObjectList.Add(new MyObject("22", "Name_22"));
 
			AddUnique(myObjectList, myDistinctList);
 
			ShowMyObjects("myObjectList", myObjectList);
			ShowMyObjects("myDistinctList", myDistinctList);
 
			myObjectList.Add(new MyObject("15", "Name_15"));
			myObjectList.Add(new MyObject("13", "Name_13"));
			myObjectList.Add(new MyObject("4", "Name_4"));
 
			AddUnique(myObjectList, myDistinctList);
 
			ShowMyObjects("myObjectList", myObjectList);
			ShowMyObjects("myDistinctList", myDistinctList);
 
			Console.ReadKey();
		}
 
		private static void ShowMyObjects(string ListName, List<MyObject> oMyObjectList)
		{
			Console.WriteLine("MyObject List: " + ListName);
 
			foreach (MyObject oMyObject in oMyObjectList)
			{
				Console.WriteLine(oMyObject);
			}
 
			Console.WriteLine();
		}
 
		private static void AddUnique(List<MyObject> oMyObjectList, List<MyObject> oMyObjectUniqueList)
		{
			foreach(MyObject oMyObject in oMyObjectList)
			{
				if (null == FindMyObjectById(oMyObjectUniqueList, oMyObject.Id)) oMyObjectUniqueList.Add(oMyObject);
			}
		}
 
		private static MyObject FindMyObjectById(List<MyObject> oMyObjectList, string Id)
		{
			foreach (MyObject oMyObject in oMyObjectList)
			{
				if (Id == oMyObject.Id) return oMyObject;
			}
 
			return null;
		}
	}
 
	class MyObject
	{
		public MyObject(string Id, string Name /*,...*/)
		{
			this.Id = Id;
			this.Name = Name;
			//...
		}
 
		public string Id;
		public string Name;
		//...
 
		public override string ToString()
		{
			return string.Format("ID: {0} - Name: {1}", this.Id, this.Name);
		}
	}
}

Open in new window

0
 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 400 total points
ID: 21786506
Hi,

you should override Equals in your 'MyObject' class as follows:

public override bool Equals(object obj)
        {
            return this.ID == ((MyObject)obj).ID;  //if your id is a string use return this.ID.Equals(((MyObject)obj).ID);
        }

then when your adding to the distinct list, use if(!List.Contains(objectToCheck)){List.Add(objectToCheck);}
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 21786520
This way when you use the List.Contains method your implementation of Equals will be used
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

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 …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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