[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

List<> distinct value

Posted on 2008-06-14
3
Medium Priority
?
10,712 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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