473,670 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Generic List FindAll method

I am new to using C# generics and I am liking what I am finding. However the
examples in online help are lacking. Can someone help me with the FindAll
method of the generic List class? As I understand the method, it will return
a list that meets the criteria evaluated in the delegate function that I use
for evaluation. I am not sure how to write this delegate, how do I get the
list item to evaluate in my delegate. Is their a defined delegate signature
or can I define my own. Specifically, I have a list of structures that
contain datapoints at various times and I waht to use FindAll to get a list
of datapoints within a defined timespan. I would send in the begin and end
times and it would return me a list of items in that time range. Any help
would be appreciated.
Nov 16 '05 #1
3 31440
Michael Rockwell <Mi************ *@discussions.m icrosoft.com> wrote:
I am new to using C# generics and I am liking what I am finding. However the
examples in online help are lacking. Can someone help me with the FindAll
method of the generic List class? As I understand the method, it will return
a list that meets the criteria evaluated in the delegate function that I use
for evaluation. I am not sure how to write this delegate, how do I get the
list item to evaluate in my delegate. Is their a defined delegate signature
or can I define my own. Specifically, I have a list of structures that
contain datapoints at various times and I waht to use FindAll to get a list
of datapoints within a defined timespan. I would send in the begin and end
times and it would return me a list of items in that time range. Any help
would be appreciated.


You need to create an implementation of Predicate<T>. For instance:

using System;
using System.Collecti ons.Generic;

class Test
{
static void Main()
{
List<string> x = new List<string>();
x.Add("Hello");
x.Add("There");
x.Add("World");
x.Add("Four");
x.Add("Three");
x.Add("Two");
x.Add("One");

foreach (string s in x.FindAll(TestF orLength5))
{
Console.WriteLi ne (s);
}
}

static bool TestForLength5 (string x)
{
return x.Length==5;
}
}

Note that the call to FindAll could have been written as

x.FindAll(new Predicate<strin g>(TestForLengt h5))

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Thanks, this helps. Can I also pass my own parameters to the Predicate
method so that I can use those parameters in the evaluation. So for example
in your example you have a hard coded length of 5, what if I wanted to pass
the length as a parameter? Can I define my own delegate signature for the
predicate?

"Jon Skeet [C# MVP]" wrote:
Michael Rockwell <Mi************ *@discussions.m icrosoft.com> wrote:
I am new to using C# generics and I am liking what I am finding. However the
examples in online help are lacking. Can someone help me with the FindAll
method of the generic List class? As I understand the method, it will return
a list that meets the criteria evaluated in the delegate function that I use
for evaluation. I am not sure how to write this delegate, how do I get the
list item to evaluate in my delegate. Is their a defined delegate signature
or can I define my own. Specifically, I have a list of structures that
contain datapoints at various times and I waht to use FindAll to get a list
of datapoints within a defined timespan. I would send in the begin and end
times and it would return me a list of items in that time range. Any help
would be appreciated.


You need to create an implementation of Predicate<T>. For instance:

using System;
using System.Collecti ons.Generic;

class Test
{
static void Main()
{
List<string> x = new List<string>();
x.Add("Hello");
x.Add("There");
x.Add("World");
x.Add("Four");
x.Add("Three");
x.Add("Two");
x.Add("One");

foreach (string s in x.FindAll(TestF orLength5))
{
Console.WriteLi ne (s);
}
}

static bool TestForLength5 (string x)
{
return x.Length==5;
}
}

Note that the call to FindAll could have been written as

x.FindAll(new Predicate<strin g>(TestForLengt h5))

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Michael Rockwell <Mi************ *@discussions.m icrosoft.com> wrote:
Thanks, this helps. Can I also pass my own parameters to the Predicate
method so that I can use those parameters in the evaluation. So for example
in your example you have a hard coded length of 5, what if I wanted to pass
the length as a parameter? Can I define my own delegate signature for the
predicate?


No. The thing to do would be something like:

public class LengthMatcher
{
int length;

public LengthMatcher (int length)
{
this.length;
}

bool Test (string x)
{
return x.Length==lengt h;
}
}

You'd then use something like:

LengthMatcher lm = new LengthMatcher(5 );
list.FindAll(lm .Test);

You could also use anonymous types if you wanted - I won't go into
those just now in case it confuses things.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

1
1877
by: Michael Rockwell | last post by:
I am just starting to use the generics collection objects and am really liking them. I am using the List object and want to use the FindAll method to return a list of items that meets my criteria. The online help does not have many examples and I am having a hard time figuring out how I need to write my delegate to do the comparisions and to return my list of items that meets the criteria set. Is the delegate signature preset or can I...
1
4337
by: Omiris | last post by:
I'm trying to use the Equals() method that is defined on the List(Of T) class. The docs seem to state that if T implements the IEquatable interface, then it will use the Equals method that is declared there in order to do an Equals() call on the List itself. So I create a class called ExportStream which implements IEquatable (and the Object.Equals method as well). Next when I put the instances of ExportStream into lists and compare, the Lists...
1
11898
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
1
2170
by: jappenzeller | last post by:
I have a class that overrides the List<T> generic class and I'm trying to use the FindAll method. I have it working fine, but the method returns a List<T> not a MyList. I guess it's easier to explain with some code. public class MyList : List<MyItem> MyList ml = new MyList(); ml = GetAList(); I want to do the following, ml = ml.FindlAll(Predicate);
7
7095
by: Abhishek | last post by:
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
1
5547
by: hardieca | last post by:
Hi, I have a list of sections that contain a parent section ID field that indicates hierarchy throughout a website. I want to filter out all non- root level sections, as indicated by a section ID of -1, so I tried using the findall method. Dim rootSectionList As New SectionList() rootSectionList = mySectionList.FindAll(AddressOf isRootSection)
2
6363
by: Alexnb | last post by:
Okay, I am not sure if there is a better way of doing this than findAll() but that is how I am doing it right now. I am making an app that screen scapes dictionary.com for definitions. However, I would like to have the type of the word for each definition. For example if def1 and def2 are noun defintions but def3 isn't: noun def1 def2
2
15925
by: Chris Kennedy | last post by:
I am getting a generic list of objects from a webservice. The list is converted to an array by this process. How do I convert the array back to a list and is there any way of finding an object within the list via one it's properties. I have seen it done in C# but not VB. E.g. obj = get object from list where obj.id = 1. I could do some kind of solution using loops but I was hoping for something a little more slick. This is for .net 2.0 by...
3
1978
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 list.find(Customer.CustomerID = 1). Is there an easy way to do this. The stuff with predicates and findall is a little confusing.
0
8814
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8592
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
7419
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
6213
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
5684
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
4211
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
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.