Solved

List<> distinct value

Posted on 2008-06-14
3
10,698 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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
gregmulvihill earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
This way when you use the List.Contains method your implementation of Equals will be used
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

11 Experts available now in Live!

Get 1:1 Help Now