473,320 Members | 2,124 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,320 software developers and data experts.

Issue with Equals on List

Hi,
I have to find an object of Employee (Say 'Empl') in a list of
objects of Employees (EmployeeList). The object Empl inturn has a list
of objects (say List of Children objects ) in it.
I have used Find method with predicate as follows,

Class Employee
{
string name;
List<Childrenchildrens; //Assume getters and setters
}
Class Children
{
string name;
int age;
}

Employee Match = EmployeeList.Find(new
comparision().FindComparision(Empl));

The predicate is something like this
public predicate<EmployeeFindComparision(Employee empl)
{
return delegate(Employee emplObj )
{
return( emplObj.Childrens.Equals(empl.Childrens));
};
}

But the issue is it always return false. I gues that we can not use
the Equals on the list the way i have used it.
Also the equals comparision that i need to do should be in the same
order. I mean if emplObj.Childrens have 3 children objects (say x, y,
z) and suppose empl.Childrens have 3 children objects (say m,n,o) then
the requirements is such that i have to check whether x is equal to m,
y is equal to n and z is equal to o.

Any inputs/code samples on how to achieve it directly using the List
or any other way like overriding the Equals method of Employee class
etc in an efficient way.

Any help is greatly apprieciated.

Thanks ,
Sachin

Jun 14 '07 #1
1 6814
sachy skrev:
Also the equals comparision that i need to do should be in the same
order. I mean if emplObj.Childrens have 3 children objects (say x, y,
z) and suppose empl.Childrens have 3 children objects (say m,n,o) then
the requirements is such that i have to check whether x is equal to m,
y is equal to n and z is equal to o.
I take it that you wish to construct an equality-predicate for a given
employee, equality being formulated as have ".Equals" children -- in
same order!

public static bool OrderedChildrenEquals(Employee a, Employee b)
{
IList<Childc_a = a.Children; IList<Childc_b = b.Children;
if (c_a.Count != c_b.Count)
return false;
for (int i = 0; i < c_a.Count; ++i)
if (!c_a[i].Equals(c_b))
return false;
return true;
}
// If you *really* want it:
public static Predicate<EmployeeOrderedChildrenEquals(Employee a)
{
return delegate(Employee b)
{ return OrderedChildrenEquals(a, b); };
}
// Or, going totally overboard:
public Predicate<EmployeeOrderedChildrenEquals()
{
return delegate(Employee b)
{ return OrderedChildrenEquals(this, b); };
}
Any inputs/code samples on how to achieve it directly using the List
or any other way like overriding the Equals method of Employee class
etc in an efficient way.
There is no more effective way of comparison than length, and then
item-by-item, without maintaining some more information.

If you are doing *many* more comparisons than updates you could look
into maintaining a hash of the children, or even maintaining an "index",
in the form of a hashmap or a prefix-tree, mapping children to parents.
But that's some pretty hefty machinery, try
the-simplest-thing-that-could-possibly-work first.

BTW: maintaining an age as a field is not a good practice, it tends to
get out of date, DateTime Birthday is a good choice :)

--
Helge
Jun 14 '07 #2

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

Similar topics

4
by: Kurt | last post by:
Wouldn't you agree all of the follwoing should produce the same result? r = (o1 == o2); r = (o2 == o1); r = object.Equals(o1, o2); r = object.Equals(o2, o1); r = (o1.Equals(o2)); r =...
5
by: matt tagliaferri | last post by:
I'm implementing a typed collection class that can't include duplicates. The Add method of the collection class is as follows: Public Function Add(ByVal o As TeamListEntry) As Integer If...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: ECathell | last post by:
I have a routine that compares 2 intances of the same object to see if there are any changes. The problem is that even though there are no changes, it tells me there are. I have even tried running a...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
1
by: Omiris | last post by:
I'm trying to use the Equals() method that is defined on the List(Of T) class. The docs seem to state that if T implements the IEquatable interface, then it will use the Equals method that is...
1
by: eramfelt | last post by:
Why doesnt ArrayList implements the Equals() or GetHashCode() methods? How is the developer supposed to check if an array list is equal to another array list? Is the developer supposed to iterate...
0
by: sachy | last post by:
Hi, I have to find an object of Employee (Say 'Empl') in a list of objects of Employees (EmployeeList). The object Empl inturn has a list of objects (say List of Children objects ) in it. I have...
5
by: blt51 | last post by:
I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.