Solved

List<> distinct value

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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