473,715 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Finding distinct elements!

On Jun 13, 2:38 pm, CSharper <cshar...@gmx.c omwrote:
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 954
On Jun 13, 9:18 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On Jun 13, 2:38 pm, CSharper <cshar...@gmx.c omwrote:
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.Equal s(p.Name);
}

Thanks,
Jun 27 '08 #2
On Jun 13, 5:10 pm, CSharper <cshar...@gmx.c omwrote:
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.co mwrote:
On Jun 13, 5:10 pm, CSharper <cshar...@gmx.c omwrote:
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.GetHa shCode();
}
Jun 27 '08 #4
On Jun 13, 5:42 pm, CSharper <cshar...@gmx.c omwrote:
Thanks again. Do you think the following is the correct implementation
of GetHashCode?

public override int GetHashCode()
{
return this.Name.GetHa shCode();
}
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 IEqualityCompar er<Person>
instead (e.g. as a NameComparer). I haven't checked whether or not
Distinct can optionally take an IEqualityCompar er, 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.co mwrote:
On Jun 13, 5:42 pm, CSharper <cshar...@gmx.c omwrote:
Thanks again. Do you think the following is the correct implementation
of GetHashCode?
public override int GetHashCode()
{
return this.Name.GetHa shCode();
}

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 IEqualityCompar er<Person>
instead (e.g. as a NameComparer). I haven't checked whether or not
Distinct can optionally take an IEqualityCompar er, 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
1931
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 statement on the customer table and orders table (joining on the ACCTNO) for all "Video" products. Ex: sq = "SELECT CUSTOMER.ACCTNO, CUSTOMER.NAME FROM CUSTOMER INNER JOIN ORDERS ON CUSTOMER.ACCTNO = ORDERS.ACCTNO WHERE PRODUCT_TYPE = 'Video' "...
3
2232
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 after that spot is found. Well, I am kinda lost on this finding things in a vector. I mean, wouldn't you just go to vector, and then ....well then I gotta find out how to do an insert in a vector. Does this involve changing the pointer to point...
3
6938
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 t1.id < t2.id
5
1537
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 records and produce output. Example <registrations> <registration> <company>Awesome Printers</company> <first>John</first>
2
4730
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 sub-elements. Heads ->(0..*)Head -> ...subelements... -> (0..*)Row The problem I have is that I find the wrong Row elements, ie I always find the FIRST Row element inside the complete Xml document. This is probably
1
1531
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 each SCE_CGPC and FSG_SNAM. I have included a achunk of my XML to help expalin this: This is a chunk of the XML file <z:row SCE_CGPC='PGRE' FSG_SNAM='HOME' MOA_NAME='Full time'
1
3336
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 att2="2" /> <row att1="1" att2="2" />
7
3390
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 narrow down the list of elements to check. I thought I could place those elements within a named DIV or a table, and then reference that DIV or table by its ID. But it's not working. Say for instance I wanted Javascript to issue an alert (just...
275
12317
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
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9198
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.