1

I tried to find out the duplicate value in a list.

However, my list's type is not primitive types, and I want to know which elements in the list are duplicate one.

For example, I have three "Person" classes in List<Person>, people, like the following codes.

My filter is "Gender" property of Person class, and the targeted result should contain "Mary" and "Sandy" objects since their Gender's values are the same-->female.

Person Paul = new Person() { Name="Paul", Gender="male", Age="15"};
Person Mary = new Person() { Name = "Mary", Gender = "female", Age = "22" };
Person Sandy = new Person() { Name = "Sandy", Gender = "female", Age = "13" };

List<Person> people = new List<Person>();
people.Add(Paul);
people.Add(Mary);
people.Add(Sandy);   
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3174976
  • 351
  • 4
  • 19
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 14 '15 at 23:51
  • BTW, don't indent your plain text. Indent your _code_ by four spaces, not your text – John Saunders Jan 14 '15 at 23:52

3 Answers3

2

Use GroupBy to find the "duplicates", then restrict by count:

var duplicates = people.GroupBy(p => p.Gender)
                       .Where (g => g.Count() >= 2);

At which point you can just enumerate:

foreach (Person person in duplicates)
{
   Console.WriteLine(person.Name);
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

Looks like you want grouping, not finding duplicates. You can do this using the LINQ GroupBy method:

people.GroupBy(p => p.Gender)

This will return an IGrouping<string, Person>, string being the group key type.

A similar method is to use a lookup:

var peopleByGender = people.ToLookup(p => p.Gender);
var females = peopleByGender["female"]; // An IEnumerable<Person> containing Mary and Sandy

This returns an ILookup<string, Person>, which is just like a dictionary, except the same key can be present multiple times.

On a side note, you should probably use an enum for the gender.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
0
people.Where(p => p.Gender.Equals("female").ToList();

Should give you what you want

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • This only handles one case. If he doesn't know what the case is he can't identify distinctness or not. – TheNorthWes Jan 14 '15 at 23:51
  • @Admiral : But in case of complex object you should know key(s) on which you filter data. – Pankaj Kapare Jan 14 '15 at 23:53
  • @PankajKapare The bigger problem is that this doesn't detect if there is more than one female or not. The condition was based on "female" occurring more than once, not just the string. – BradleyDotNET Jan 14 '15 at 23:55
  • Eh I disagree with that sentiment. What if your users are adding their location to their profile and you just want to know if you are underpenetrated into some location by looking for unique cities. I know what you are saying, but it can't fit all situations, least of all individuals. – TheNorthWes Jan 14 '15 at 23:55