473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Re: Finding distinct elements!

On Jun 13, 2:38 pm, CSharper <cshar...@gmx.comwrote:
here is the code
<snip>

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

Jon
Jun 27 '08 #1
5 945
On Jun 13, 9:18 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 13, 2:38 pm, CSharper <cshar...@gmx.comwrote:
here is the code

<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,
Jun 27 '08 #2
On Jun 13, 5:10 pm, CSharper <cshar...@gmx.comwrote:
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
Jun 27 '08 #3
On Jun 13, 11:23 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 13, 5:10 pm, CSharper <cshar...@gmx.comwrote:
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
Hi Jon,

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

public override int GetHashCode()
{
return this.Name.GetHashCode();
}
Jun 27 '08 #4
On Jun 13, 5:42 pm, CSharper <cshar...@gmx.comwrote:
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
Jun 27 '08 #5
On Jun 13, 11:58 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 13, 5:42 pm, CSharper <cshar...@gmx.comwrote:
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
Thank you very much, you gave me good direction for me to take
further.
As always thank you for the help.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Berry | last post by:
Hi All. I'm trying to write an ASP page that shows me the UNIQUE account number for a customer (so I can pass it to another page) based on a search criteria. For example, I want to do a select...
3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
5
by: Chris Kettenbach | last post by:
Good Morning, Sorry for xposting. Just need a liitle help. I have an xml file that's generated from a database. How do I select distinct values from a field in xslt and then loop through the...
2
by: TT (Tom Tempelaere) | last post by:
Hey there, I'm having trouble finding the right XPath expression. The XML file I'm parsing contains an element Heads which contains sereveral Head elements. These Head elements have several Row...
1
by: Chris | last post by:
I have a problem with grouping elements in my XSL. What I want to do is select all of the distinct SCE_CGPC records, then by the FSG_SNAM records for each SCE_CGPC and then again by MOA_NAME for...
1
by: Damien | last post by:
I have seen examples of selecting distinct elements by name, but my issue is that I need to get distinct attributes by name. My XML looks like: <root> <file> <rows> <row att1="1" /> <row...
7
by: stevewy | last post by:
I'm looking to manipulate/check the state of various checkboxes and radios in a form. Because I don't want to access all of the checkboxes in the form, only a specific section, I am trying to...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.