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

Find Method on Generic List

I have a generic collection of objects and I would like to find the object
by one of it's properties e.g. I would like something with the functionality
of something like list.find(Customer.CustomerID = 1). Is there an easy way
to do this. The stuff with predicates and findall is a little confusing.
Sep 7 '08 #1
3 1968
Fresno Bob wrote:
I have a generic collection of objects and I would like to find the object
by one of it's properties e.g. I would like something with the functionality
of something like list.find(Customer.CustomerID = 1). Is there an easy way
to do this. The stuff with predicates and findall is a little confusing.
I hope this cures some of your confusion:

The FindAll method uses a delegate, so you can just make a method and
use with the FindAll method:

public static bool FindCustomerOne(Customer c) {
return c.CustomerID == 1;
}

List<Customerresult = myCustomerList.FindAll(FindCustomerOne);

The FindAll method will just loop through all the items in the list and
call the FindCustomerOne method for each one to determine which one to
return in the result.
You can also do the same with an anonymous method:

List<Customerresult = myCustomerList.FindAll(
delegate(Customer c){ return c.CustomerID == 1; }
);
If you are using C# 3, you can also use a lambda expression to do the same:

List<Customerresult = myCustomerList.FindAll(c =c.CustomerID == 1);

--
Göran Andersson
_____
http://www.guffa.com
Sep 7 '08 #2
Sorry about the C# code. Didn't look to carefully where I was. You can
do the same with VB using the AddressOf keyword to send a delegate to
the FindAll method.

--
Göran Andersson
_____
http://www.guffa.com
Sep 7 '08 #3
Bob,

This is in detail (in my idea very well) described on MSDN

http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

Cor
"Fresno Bob" <no****@nospam.comschreef in bericht
news:ec**************@TK2MSFTNGP04.phx.gbl...
>I have a generic collection of objects and I would like to find the object
by one of it's properties e.g. I would like something with the
functionality of something like list.find(Customer.CustomerID = 1). Is
there an easy way to do this. The stuff with predicates and findall is a
little confusing.

Sep 8 '08 #4

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

Similar topics

6
by: Paul Welter | last post by:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a...
2
by: Richard Bysouth | last post by:
I'm sure this is something pretty trivial but can't seem to figure out how to do it. I'm using Generic Lists in quite a few places in my app and for some of them want to be able to create a...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
2
by: D2 | last post by:
Hi, I have a requirement where I need to call a generic method without knowing the class name i.e. I'm getting class name from xml file. Given below is a replica of the scenario we are having. ...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
1
by: Fresno Bob | last post by:
I have a generic collection of objects and I would like to find the object by one of it's properties e.g. I would like something with the functionality of something like...
3
by: Tony Johansson | last post by:
Hello! Assume I have the class shown below and it's stored in a generic List. If I want to find the Value for a specific Id how do I use the find method. We can assume that the generic List...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.