473,698 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Employeean d 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 IsGreaterThen20 00(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 7099
Abhishek,

Well, the FindAll method takes a Predicate<Tdele gate. 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 IsGreaterThan20 00
method. However, I think that in this case, anonymous delegates is easier
(if the logic is easy):

List<Tresults = list.FindAll(
delegate(Employ ee 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(Employ ee 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.co m

"Abhishek" <lu***********@ gmail.comwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.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<Employeean d 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 IsGreaterThen20 00(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.guar d.caspershouse. comwrote:
Abhishek,

Well, theFindAllmetho dtakes a Predicate<Tdele gate. 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 IsGreaterThan20 00method. However, I think that in this case, anonymous delegates is easier
(if the logic is easy):

List<Tresults =list.FindAll(
delegate(Employ ee 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(Employ ee 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.c om

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

news:11******** **************@ h2g2000hsg.goog legroups.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<Employeea nd 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 IsGreaterThen20 00(Employee obj)
{
//some logic
return true/false;
}
and then pass this as predicate toFindAllmethod 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 genericListclas s
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<Employeewh ose salary is
more then salaryobject.Am ount
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<Employeean d 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 IsGreaterThen20 00(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<Employeecr eam = employees.FindA ll(comparer.Hig her);
List<Employeero ots = employees.FindA ll(comparer.Low er);

--
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<Employeere sult = list.FindAll(
delegate(Employ ee 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.co m

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

Well, theFindAllmetho dtakes a Predicate<Tdele gate. 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
IsGreaterThan2 000method. However, I think that in this case, anonymous
delegates is easier
(if the logic is easy):

List<Tresult s =list.FindAll(
delegate(Employ ee 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<Tresult s =list.FindAll(
delegate(Employ ee 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.c om

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

news:11******* *************** @h2g2000hsg.goo glegroups.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<Employeea nd 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 IsGreaterThen20 00(Employee obj)
{
//some logic
return true/false;
}
and then pass this as predicate toFindAllmethod 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 genericListclas s
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<Employeewh ose salary is
more then salaryobject.Am ount
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.guar d.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<Employeere sult = list.FindAll(
delegate(Employ ee 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.c om

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

news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
On May 9, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
Abhishek,
Well, theFindAllmetho dtakes a Predicate<Tdele gate. 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
IsGreaterThan20 00method. However, I think that in this case, anonymous
delegates is easier
(if the logic is easy):
List<Tresults =list.FindAll(
delegate(Employ ee 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(Employ ee 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.c om
"Abhishek" <luckyabhis...@ gmail.comwrote in message
>news:11******* *************** @h2g2000hsg.goo glegroups.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<Employeea nd 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 IsGreaterThen20 00(Employee obj)
{
//some logic
return true/false;
}
and then pass this as predicate toFindAllmethod 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 genericListclas s
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<Employeewh ose salary is
more then salaryobject.Am ount
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
15175
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 generic list...is not clear to me. Any suggestions? System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(); foreach(string s in myAnotherArray)
2
2087
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 floats. I basically require the fastest way to convert a managed array of floats into an unmanaged array of floats. As far as the managed collection is implemented, ive read that the
8
4960
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
12840
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 the List<T> type. Could some one tell me where can I find the IList implementation? Please note that the plase where I am looking for this implementation is from the Visula Studio metadata information page. The page that shows up when you...
3
2126
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 we have an integer list p, say. For each element x in p, we want to repace x with f(x) to get a new, possibly larger, integer list. Note
4
2950
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: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the returned value.
5
3839
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 connectivity for basic operations and such. I've ran into a snag that I just can't think myself out of. Here's an example: I have an object for a Student called StudentRecord. It has properties such as name, grade, identification number, etc.
2
2252
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 counter to provide me a count. How can I refactor the same function w/o the foreach and use findall instead? faux data structure: --------------------- -> | Customers
6
2931
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 TestClass { public int i = 0; public int IValue
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
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
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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

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.