Finding distinct elements!

  • Thread starter Thread starter CSharper
  • Start date Start date
C

CSharper

I have a class like the following
Person
- Name
- Age
- Sex
from there I create an array of the same class. Now I need to find out
all the persons with unique name in it along with age and sex,
duplicate name needs to be dropped. Well not a good example want to
learn how the distinct work

Person[] uniqueList =
GetAllPersonNames().case<Person>().ToArray<Person>().Distinct()

it didn't work. GetAppPersonNames return IEnumerable of Person.

Thanks.
 
I have a class like the following
Person
- Name
- Age
- Sex
from there I create an array of the same class. Now I need to find out
all the persons with unique name in it along with age and sex,
duplicate name needs to be dropped. Well not a good example want to
learn how the distinct work

Person[] uniqueList =
GetAllPersonNames().case<Person>().ToArray<Person>().Distinct()

it didn't work. GetAppPersonNames return IEnumerable of Person.

What's the case() method meant to do?

Distinct() will drop people who are duplicate according to the
hashcode and equality of your Person class. In other words, assuming
you've implemented them in the natural way, it would keep duplicate
names so long as the age or gender were different.

If that's not the problem, please post a short but complete example,
or at least give us more information that "it didn't work".

Jon
 
I have a class like the following
Person
- Name
- Age
- Sex
from there I create an array of the same class. Now I need to find out
all the persons with unique name in it along with age and sex,
duplicate name needs to be dropped. Well not a good example want to
learn how the distinct work
Person[] uniqueList =
GetAllPersonNames().case<Person>().ToArray<Person>().Distinct()
it didn't work. GetAppPersonNames return IEnumerable of Person.

What's the case() method meant to do?

Distinct() will drop people who are duplicate according to the
hashcode and equality of your Person class. In other words, assuming
you've implemented them in the natural way, it would keep duplicate
names so long as the age or gender were different.

If that's not the problem, please post a short but complete example,
or at least give us more information that "it didn't work".

Jon

here is the code

class Program
{
Person[] people;

public class Person
{
public string Name {get; set;}
public int age {get; set;}
public string sex {get; set;}
}
static void Main(string[] args)
{
Program pg = new Program();
pg.CreatePerson();
pg.DisplayDistinct();
}

private void DisplayDistinct()
{
Person[] list =
GetPerson().Distinct<Person>().ToArray<Person>();
foreach (Person p in list)
Console.WriteLine(p.Name);
Console.ReadKey();
}

private IEnumerable<Person> GetPerson()
{
foreach (Person p in people)
yield return p;
}

private void CreatePerson()
{
people = new Person[8];
for (int i = 0; i < 6; i++)
{
people = new Person() { Name = i.ToString(), age =
20, sex = "M" };
}
people[6] = new Person { Name = "1", age = 20, sex =
"M" };
people[7] = new Person { Name = "2", age = 20, sex =
"M" };
}
}
 
<snip>

Your Person class doesn't override Equals or GetHashCode. Therefore
every Person is distinct.

Jon

Hi Jon,

Thanks and I have added following method and put a break point in the
method and I noticed the method never get called.

public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
Person p = (Person)obj;
return this.Name.Equals(p.Name);
}

Thanks,
 
Thanks and I have added following method and put a break point in the
method and I noticed the method never get called.

<snip>

Did you override GetHashCode as well? If not, each Person would
(probably) have a distinct hash code, and thus the Distinct() method
would quite reasonably assume they weren't equal, without ever calling
your Equals method.

Jon
 
<snip>

Did you override GetHashCode as well? If not, each Person would
(probably) have a distinct hash code, and thus the Distinct() method
would quite reasonably assume they weren't equal, without ever calling
your Equals method.

Jon

Hi Jon,

Thanks again. Do you think the following is the correct implementation
of GetHashCode?

public override int GetHashCode()
{
return this.Name.GetHashCode();
}
 
Thanks again. Do you think the following is the correct implementation
of GetHashCode?

public override int GetHashCode()
{
return this.Name.GetHashCode();
}

That's fine as long as Name isn't null.

Are you sure that you really want to only judge equality by name
though? Why not include age and gender? (Date of birth is generally
better than age, btw - it doesn't change over time.) If this is just
for the purposes of learning about Distinct() that's fine, but
otherwise you might want to implement IEqualityComparer<Person>
instead (e.g. as a NameComparer). I haven't checked whether or not
Distinct can optionally take an IEqualityComparer, but I'd be
surprised if it couldn't.

Jon
 
That's fine as long as Name isn't null.

Are you sure that you really want to only judge equality by name
though? Why not include age and gender? (Date of birth is generally
better than age, btw - it doesn't change over time.) If this is just
for the purposes of learning about Distinct() that's fine, but
otherwise you might want to implement IEqualityComparer<Person>
instead (e.g. as a NameComparer). I haven't checked whether or not
Distinct can optionally take an IEqualityComparer, but I'd be
surprised if it couldn't.

Jon

Thank you very much, you gave me good direction for me to take
further.
As always thank you for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top