Solved

List<> distinct value

Posted on 2008-06-14
3
10,707 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 150 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 100 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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