473,378 Members | 1,370 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,378 software developers and data experts.

FindAll method with List<T> generic class

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<Employeeand 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

May 9 '07 #1
7 7078
Abhishek,

Well, the FindAll method takes a Predicate<Tdelegate. 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<Tresults = 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<Tresults = 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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Abhishek" <lu***********@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
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<Employeeand 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

May 9 '07 #2
On May 9, 4:31 pm, Abhishek <luckyabhis...@gmail.comwrote:

<snip>
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

May 9 '07 #3
On May 9, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Abhishek,

Well, theFindAllmethodtakes a Predicate<Tdelegate. 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<Tresults =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<Tresults =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]
- m...@spam.guard.caspershouse.com

"Abhishek" <luckyabhis...@gmail.comwrote in message

news:11**********************@h2g2000hsg.googlegro ups.com...
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<Employeeand 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<Employeewhose 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

May 9 '07 #4
Abhishek wrote:
Now i am having a List<Employeeand 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<Employeecream = employees.FindAll(comparer.Higher);
List<Employeeroots = employees.FindAll(comparer.Lower);

--
Göran Andersson
_____
http://www.guffa.com
May 9 '07 #5
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<Employeeresult = 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]
- mv*@spam.guard.caspershouse.com

"Abhishek" <lu***********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On May 9, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>Abhishek,

Well, theFindAllmethodtakes a Predicate<Tdelegate. 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<Tresults =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<Tresults =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]
- m...@spam.guard.caspershouse.com

"Abhishek" <luckyabhis...@gmail.comwrote in message

news:11**********************@h2g2000hsg.googlegr oups.com...
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<Employeeand 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<Employeewhose 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

May 9 '07 #6
On May 9, 9:05 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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<Employeeresult = 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]
- m...@spam.guard.caspershouse.com

"Abhishek" <luckyabhis...@gmail.comwrote in message

news:11**********************@q75g2000hsh.googlegr oups.com...
On May 9, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Abhishek,
Well, theFindAllmethodtakes a Predicate<Tdelegate. 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<Tresults =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<Tresults =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]
- m...@spam.guard.caspershouse.com
"Abhishek" <luckyabhis...@gmail.comwrote in message
>news:11**********************@h2g2000hsg.googlegr oups.com...
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<Employeeand 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<Employeewhose 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

May 10 '07 #7
I think custom iterators is a good solution either:
http://msdn2.microsoft.com/vcsharp/bb264519.aspx

Moty
May 10 '07 #8

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

Similar topics

3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
2
by: Daniel Mori | last post by:
Hi, Im hoping someone can give me some advice. Im doing some development using VS Whidbey 2005 beta 1. Im about to implement some highly time critical code related to a managed collection of...
8
by: Vivek | last post by:
Hi, I wish to update the LIST<T> created in PARENT FORM from the CHILD FORM. Currently I have declared the LIST<Role> as public in my parent form. What can I do to update the <LIST>? Thanks
4
by: Rene | last post by:
According to the documentation, the List<T> type explicitly implements the non generic IList interface. The problem is that no matter how hard I look, I am not able to find this implemetion on...
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
2
by: =?Utf-8?B?bWdvbnphbGVzMw==?= | last post by:
I have a List<tobject consisting of objects which in themselves consist of BindingListViews of objects. When I want to search for a object value I normally create a foreach loop and increment a...
6
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class...
1
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: 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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.