[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

List<> distinct value

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 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