1

I have a class ClientState

Class ClientState
{
Public int ID{get;set;}
public string State{get;set;}
}

List<ClientState> listClientState which contain all states of USA, Now may problem is listClientState contain some objects which have duplicates states. How can i filter listClientState to remove duplicate record

Jens
  • 25,229
  • 9
  • 75
  • 117
Pankaj
  • 4,419
  • 16
  • 50
  • 72
  • 1
    Can you have two ClientStates with different IDs but with the same State? If so, which of the two IDs would you want to keep in the list after the duplicates have been removed? – Daniel Renshaw Apr 30 '10 at 10:10

4 Answers4

2

You could write a comparer

class ClientStatesComparer : IEqualityComparer<ClientState>
{
    public bool Equals(ClientState x, ClientState y)
    {
        return x.State = y.State;
    }

    public int GetHashCode(ClientState obj)
    {
        return obj.State;
    }
}

and use

listClientState = listClientState.Distinct(new ClientStatesComparer()).ToList();

You will of course loose records (i.e. loose the ID) this way. If every ID is unique to a state, Veers solution will do it.

Jens
  • 25,229
  • 9
  • 75
  • 117
1

did you try listClientState.Distinct().ToList()

Amsakanna
  • 12,254
  • 8
  • 46
  • 58
0

You could use a HashSet<>, which doesn't allow duplicates (it simply ignores them on Add()). Besides the posted answers, you can also create a HashSet from a List, and all the duplicates are ignored:

HashSet<ClientState> noDupes = new HashSet<ClientState>(listWithDupes);
List<ClientState> listNoDupes = noDupes.ToList();
Daniel Rose
  • 17,233
  • 9
  • 65
  • 88
0

VERY Simple example:

public class ClientState : IComparable<ClientState>
{
    public int ID { get; set; }
    public string State { get; set; }

    public override string ToString()
    {
        return String.Format("ID: {0}, State: {1}", ID, State);
    }

    #region IComparable<ClientState> Members

    public int CompareTo(ClientState other)
    {
        return other.State.CompareTo(State);
    }

    #endregion
}

static void Main(string[] args)
        {

            List<ClientState> clientStates = new List<ClientState>
                                                 {
                                                     new ClientState() {ID = 1, State = "state 1"},
                                                     new ClientState() {ID = 2, State = "state 1"},
                                                     new ClientState() {ID = 4, State = "state 3"},
                                                     new ClientState() {ID = 11, State = "state 2"},
                                                     new ClientState() {ID = 14, State = "state 1"},
                                                     new ClientState() {ID = 15, State = "state 2"},
                                                     new ClientState() {ID = 21, State = "state 1"},
                                                     new ClientState() {ID = 20, State = "state 2"},
                                                     new ClientState() {ID = 51, State = "state 1"}
                                                 };

            removeDuplicates(clientStates);
            Console.ReadLine();
        }

        private static void removeDuplicates(IList<ClientState> clientStatesWithPossibleDuplicates)
        {
            for (int i = 0; i < clientStatesWithPossibleDuplicates.Count; ++i)
            {
                for (int j = 0; j < clientStatesWithPossibleDuplicates.Count; ++j)
                {
                    if (i != j)
                    {
                        if (clientStatesWithPossibleDuplicates[i].CompareTo(clientStatesWithPossibleDuplicates[j]) == 0)
                        {
                            clientStatesWithPossibleDuplicates.RemoveAt(i);
                            i = 0;
                        }
                    }
                }
            }
        }

Output:

ID: 4, State: state 3
ID: 20, State: state 2
ID: 51, State: state 1
Lukas Šalkauskas
  • 14,191
  • 20
  • 61
  • 77