The equivalent of select distinct in .Net 2.0
So a while back my bud Ewoudt Kellerman asked me how to take a list of strings in .Net and get a distinct list from them.At the time i was busy and gave a kind of half hearted answer that while it got the job done was long and not very autonomous .I had a few minutes today and whipped this up an thought i would share it with everyone that was looking for a good example on how to use Predicates and Deletegates. I must say before i most this code that it is not the greatest and more a proof of concept and might need some adaptation to use .Also i know there are beter ways to do this in .Net 3.5 and linq but i needed it in .Net 2.0 and i think he did as well.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
///
/// Please dont kill me about the naming or
/// any other standards , this was just a quick
/// class to demonstrate proof of concept.
/// There are surely beter and newer ways to do it
/// in .net 3.5 but this is aimed at the .net 2.0 crowd.
/// Thanks
/// Jameel
///
public class DisctintTest
{
///
/// Constructor For the class nothing special here
/// just calls the test method automatically on creation
/// so that testing is quicker.
///
public DisctintTest()
{
Test();//Call the method
}
//This will contain the last checked string ,starts of as blank
private string lastItem = "";
//The output data that can be inspected to view the cleaned data if u actually want to run this
//example
public List outputData;
///
/// The test method
///
private void Test()
{
//Create a new string list to load with values.
//By using a string list u can add strings that will be sorted
//alphabetically or use numbers which will be sorted by order
List myList = new List();
//Add some data
myList.Add("a");
myList.Add("a");
myList.Add("c");
myList.Add("c");
myList.Add("b");
myList.Add("d");
myList.Add("b");
myList.Add("c");
myList.Add("e");
myList.Add("f");
myList.Add("1");
myList.Add("1");
myList.Add("2");
myList.Add("3");
myList.Add("3");
myList.Add("2");
myList.Add("1");
myList.Add("4");
myList.Add("5");
myList.Add("2");
//Sort the data so that when we remove the data comparisons are easy
myList.Sort();
//Use the removeall to remove the duplicates by using a predicate
//that calls a delegate.
myList.RemoveAll(new Predicate(RemoveDuplicates));
//Set the output data
outputData = myList;
}
///
/// Removes Duplicates from the list based on the last item.
/// It is important to use the sort first or the end list
/// will not be entirely cleaned.
///
///
///
private bool RemoveDuplicates(string currentItem)
{
//Check if the current item we are comapring is the same as the last.
//If the list was sorted and there was a duplicate this would be true.
//And the list would remove the current item
if (currentItem == lastItem)
{
return true;
}
//If not continue and set the last item to the curent item
lastItem = currentItem;
//Return False so as not to delete the current item.
return false;
}
}







