FindAll method with List<T> generic class

A

Abhishek

Hi

I am using a generic list in my program to have a list of objects
stored. I want to compare all the objects in this list with object of
another class and find all the objects which meet the criteria.
To make it more specific
I have a class Employee and a class Salary which look like
Employee
{
Name
Salary
Age
Id
}

Salary
{
Currency
Amount
}

Now i am having a List<Employee> and when now what i want is to get a
List of Employees whose salary is greater then 2000.
One way to achieve this is to write a method in class Salary which
looks like
public bool IsGreaterThen2000(Employee obj)
{
//some logic
return true/false;
}

and then pass this as predicate to FindAll method however the
responsibility of comparison lies with Salary class. I want to shift
this responsibility to some other class which already has the logic
for comparison.
Is there a way to achieve this using existing generic List class
in .NET framework.

Any help would be highly appreciated.

Thanks, Abhishek
 
J

Jon Skeet [C# MVP]

and then pass this as predicate to FindAll method however the
responsibility of comparison lies with Salary class. I want to shift
this responsibility to some other class which already has the logic
for comparison.
Is there a way to achieve this using existing generic List class
in .NET framework.

It's not clear where you want the logic, but so long as you've got an
accessible method with the right signature that does the right thing
with its input, you can use that to create the Predicate<T>.

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

Abhishek,

Well, the FindAll method takes a Predicate<T> delegate. In this case,
your T is the Employee class. If your logic can operate on only information
from the Employee class, then you can easily use the IsGreaterThan2000
method. However, I think that in this case, anonymous delegates is easier
(if the logic is easy):

List<T> results = list.FindAll(
delegate(Employee e)
{
return e.Salary.Amount > 2000;
});

If you have a requirement for more information, you can use the fact
that anonymous delegates can reference outside data to call your method:

List<T> results = list.FindAll(
delegate(Employee e)
{
// Make a call to a method here, or perform logic which also uses
variables
// from outside of the delegate.
});

Hope this helps.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Abhishek said:
Now i am having a List<Employee> and when now what i want is to get a
List of Employees whose salary is greater then 2000.
One way to achieve this is to write a method in class Salary which
looks like
public bool IsGreaterThen2000(Employee obj)
{
//some logic
return true/false;
}

and then pass this as predicate to FindAll method however the
responsibility of comparison lies with Salary class. I want to shift
this responsibility to some other class which already has the logic
for comparison.
Is there a way to achieve this using existing generic List class
in .NET framework.

You can place the method that you use as predicate in any class. You can
for example make a special class for comparing salary:

public class SalaryComparer {

private int _salary;

public SalaryComparer(salary) {
_salary = salary;
}

public bool Higher(Employee e) {
return e.Salary >= _salary;
}

public bool Lower(Employee e) {
return e.Salary < _salary;
}

}

SalaryComparer comparer = new SalaryComparer(2000);
List<Employee> cream = employees.FindAll(comparer.Higher);
List<Employee> roots = employees.FindAll(comparer.Lower);
 
A

Abhishek

Abhishek,

Well, theFindAllmethodtakes a Predicate<T> delegate. In this case,
your T is the Employee class. If your logic can operate on only information
from the Employee class, then you can easily use the IsGreaterThan2000method. However, I think that in this case, anonymous delegates is easier
(if the logic is easy):

List<T> results =list.FindAll(
delegate(Employee e)
{
return e.Salary.Amount > 2000;
});

If you have a requirement for more information, you can use the fact
that anonymous delegates can reference outside data to call yourmethod:

List<T> results =list.FindAll(
delegate(Employee e)
{
// Make a call to amethodhere, or perform logic which also uses
variables
// from outside of the delegate.
});

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I am using a genericlistin my program to have alistof objects
stored. I want to compare all the objects in thislistwith object of
another class and find all the objects which meet the criteria.
To make it more specific
I have a class Employee and a class Salary which look like
Employee
{
Name
Salary
Age
Id
}

Now i am having aList<Employee> and when now what i want is to get a
Listof Employees whose salary is greater then 2000.
One way to achieve this is to write amethodin class Salary which
looks like
public bool IsGreaterThen2000(Employee obj)
{
//some logic
return true/false;
}
and then pass this as predicate toFindAllmethodhowever the
responsibility of comparison lies with Salary class. I want to shift
this responsibility to some other class which already has the logic
for comparison.
Is there a way to achieve this using existing genericListclass
in .NET framework.
Any help would be highly appreciated.
Thanks, Abhishek- Hide quoted text -

- Show quoted text -

Hi Nicholas

Thanks for the quick response
However the problem is that for the comparison to be done i need both
the objects. I have a feeling that i have phrased the problem
incorrectly.
To rephrase let's have something like
Employee
{
Amount
Name
}

Salary
{
Amount
Currency
}

Now i want to find all the objects in List<Employee> whose salary is
more then salaryobject.Amount
So for comparison purpose i have to have a predicate written inside
the Salary class. I can't see a way in existing List to actually use
anonymous delegate.

~Abhishek
 
N

Nicholas Paldino [.NET/C# MVP]

Abhishek,

That's the thing though, you have to have something that correlates the
Employee instance to the Salary instance. Even if you have the
corresponding one. For example, if you had something like this:

// Initialize this with the appropriate salary.
Salary s = ...;

// Find all employees where the salary is less than the salary in s
List<Employee> result = list.FindAll(
delegate(Employee e)
{
// Return true if the salary is less than the salary in s.
return e.Salary <= s.Salary;
});


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Abhishek said:
Abhishek,

Well, theFindAllmethodtakes a Predicate<T> delegate. In this case,
your T is the Employee class. If your logic can operate on only
information
from the Employee class, then you can easily use the
IsGreaterThan2000method. However, I think that in this case, anonymous
delegates is easier
(if the logic is easy):

List<T> results =list.FindAll(
delegate(Employee e)
{
return e.Salary.Amount > 2000;
});

If you have a requirement for more information, you can use the fact
that anonymous delegates can reference outside data to call yourmethod:

List<T> results =list.FindAll(
delegate(Employee e)
{
// Make a call to amethodhere, or perform logic which also uses
variables
// from outside of the delegate.
});

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I am using a genericlistin my program to have alistof objects
stored. I want to compare all the objects in thislistwith object of
another class and find all the objects which meet the criteria.
To make it more specific
I have a class Employee and a class Salary which look like
Employee
{
Name
Salary
Age
Id
}

Now i am having aList<Employee> and when now what i want is to get a
Listof Employees whose salary is greater then 2000.
One way to achieve this is to write amethodin class Salary which
looks like
public bool IsGreaterThen2000(Employee obj)
{
//some logic
return true/false;
}
and then pass this as predicate toFindAllmethodhowever the
responsibility of comparison lies with Salary class. I want to shift
this responsibility to some other class which already has the logic
for comparison.
Is there a way to achieve this using existing genericListclass
in .NET framework.
Any help would be highly appreciated.
Thanks, Abhishek- Hide quoted text -

- Show quoted text -

Hi Nicholas

Thanks for the quick response
However the problem is that for the comparison to be done i need both
the objects. I have a feeling that i have phrased the problem
incorrectly.
To rephrase let's have something like
Employee
{
Amount
Name
}

Salary
{
Amount
Currency
}

Now i want to find all the objects in List<Employee> whose salary is
more then salaryobject.Amount
So for comparison purpose i have to have a predicate written inside
the Salary class. I can't see a way in existing List to actually use
anonymous delegate.

~Abhishek
 
A

Abhishek

Abhishek,

That's the thing though, you have to have something that correlates the
Employee instance to the Salary instance. Even if you have the
corresponding one. For example, if you had something like this:

// Initialize this with the appropriate salary.
Salary s = ...;

// Find all employees where the salary is less than the salary in s
List<Employee> result = list.FindAll(
delegate(Employee e)
{
// Return true if the salary is less than the salary in s.
return e.Salary <= s.Salary;
});

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Abhishek,
Well, theFindAllmethodtakes a Predicate<T> delegate. In this case,
your T is the Employee class. If your logic can operate on only
information
from the Employee class, then you can easily use the
IsGreaterThan2000method. However, I think that in this case, anonymous
delegates is easier
(if the logic is easy):
List<T> results =list.FindAll(
delegate(Employee e)
{
return e.Salary.Amount > 2000;
});
If you have a requirement for more information, you can use the fact
that anonymous delegates can reference outside data to call yourmethod:
List<T> results =list.FindAll(
delegate(Employee e)
{
// Make a call to amethodhere, or perform logic which also uses
variables
// from outside of the delegate.
});
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi
I am using a genericlistin my program to have alistof objects
stored. I want to compare all the objects in thislistwith object of
another class and find all the objects which meet the criteria.
To make it more specific
I have a class Employee and a class Salary which look like
Employee
{
Name
Salary
Age
Id
}
Salary
{
Currency
Amount
}
Now i am having aList<Employee> and when now what i want is to get a
Listof Employees whose salary is greater then 2000.
One way to achieve this is to write amethodin class Salary which
looks like
public bool IsGreaterThen2000(Employee obj)
{
//some logic
return true/false;
}
and then pass this as predicate toFindAllmethodhowever the
responsibility of comparison lies with Salary class. I want to shift
this responsibility to some other class which already has the logic
for comparison.
Is there a way to achieve this using existing genericListclass
in .NET framework.
Any help would be highly appreciated.
Thanks, Abhishek- Hide quoted text -
- Show quoted text -
Hi Nicholas
Thanks for the quick response
However the problem is that for the comparison to be done i need both
the objects. I have a feeling that i have phrased the problem
incorrectly.
To rephrase let's have something like
Employee
{
Amount
Name
}

Now i want to find all the objects in List<Employee> whose salary is
more then salaryobject.Amount
So for comparison purpose i have to have a predicate written inside
the Salary class. I can't see a way in existing List to actually use
anonymous delegate.
~Abhishek

Hi Nicholas

I think i got your point and this solves my concern.

Thanks a lot for all the comments and replies to everyone involved in
the discussion.

~Abhishek
 

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