Tuesday, 13 August 2013

Making a list disctinct

Making a list disctinct

I have an object type 'A' that contains a list of key value pairs.
The key value pairs is a category string and a value string.
To instantiate object type A, I would have to do the following:
List<KeyValuePair> keyValuePairs = new List<KeyValuePair>();
keyValuePairs.Add(new KeyValuePair<"Country", "U.S.A">());
keyValuePairs.Add(new KeyValuePair<"Name", "Mo">());
keyValuePairs.Add(new KeyValuePair<"Age", "33">());
A a = new A(keyValuePairs);
Eventually, I will have a List of A object types and I want to manipulate
the list so that i only get unique values and I base it only on the
country name. Therefore, I want the list to be reduced to only have ONE
"Country", "U.S.A", even if it appears more than once.
I was looking into the linq Distinct, but it does not do what I want
because it I can't define any parameters and because it doesn't seem to be
able to catch two equivalent objects of type A. I know that I can override
the "Equals" method, but it still doesn't solve the my problem, which is
to render the list distinct based on ONE of the key value pairs.

No comments:

Post a Comment