Issue with Equals on List

S

sachy

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<Children> childrens; //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<Employee> FindComparision(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
 
H

Helge Jensen

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<Child> c_a = a.Children; IList<Child> c_b = b.Children;
if (c_a.Count != c_b.Count)
return false;
for (int i = 0; i < c_a.Count; ++i)
if (!c_a.Equals(c_b))
return false;
return true;
}
// If you *really* want it:
public static Predicate<Employee> OrderedChildrenEquals(Employee a)
{
return delegate(Employee b)
{ return OrderedChildrenEquals(a, b); };
}
// Or, going totally overboard:
public Predicate<Employee> OrderedChildrenEquals()
{
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 :)
 

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

Top