Working with FindAll() and predicates.

D

DagoFlores

Hi, let's take a look to this code that we already know.:

List<string> dinosaurs = new List<string>();

dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
....
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
....
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s){

Is a list of strings, and find all items that make match with the
predicate EndsWithSaurus.

But, what about if I want to work with another type than String or
Integer types ? I mean that an object of type Animal that contains
properties that I can do the FindAll method like FindAll(Animal,
HasWings) then the predicate check if the animal has wings and return
true.

In other words, there is any way to change the rigid comparison with
constants ("saurus") by thisMatch ?

private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{

like this :

private static bool EndsWithSaurus(String s, String thisMatch)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == thisMatch))
{

Thank you,
 
J

Jon Shemitz

DagoFlores said:
But, what about if I want to work with another type than String or
Integer types ? I mean that an object of type Animal that contains
properties that I can do the FindAll method like FindAll(Animal,
HasWings) then the predicate check if the animal has wings and return
true.

FindAll takes a Predicate<T> - a delegate to a method that takes a
list element and returns a boolean. In other words, just pass in a
predicate that takes an Animal instead of a predicate that takes a
string.
In other words, there is any way to change the rigid comparison with
constants ("saurus") by thisMatch ?

private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{

Fwiw, you should take a look at the String.EndsWith method - there is
a overload that can be case-insensitive.
private static bool EndsWithSaurus(String s, String thisMatch)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == thisMatch))
{

I'm probably not understanding your question, because I see no link
between a List<Animal> and your "In other words."

You want to write a parameterized predicate? One that can doesn't
hard-code the EndsWith string? Try an anonymous method - something
like

List<string> EndsWith(List<string> Strings, string Target)
{
return Strings.FindAll(delegate (string S)
{
return S.EndsWith(Target,
StringComparison.CurrentCultureIgnoreCase);
} );
}
 
P

Pete Sambras

DagoFlores said:
Hi, let's take a look to this code that we already know.:

List<string> dinosaurs = new List<string>();

dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
...
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
...
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s){

Is a list of strings, and find all items that make match with the
predicate EndsWithSaurus.

But, what about if I want to work with another type than String or
Integer types ? I mean that an object of type Animal that contains
properties that I can do the FindAll method like FindAll(Animal,
HasWings) then the predicate check if the animal has wings and return
true.

In other words, there is any way to change the rigid comparison with
constants ("saurus") by thisMatch ?

private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{

like this :

private static bool EndsWithSaurus(String s, String thisMatch)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == thisMatch))
{

Thank you,

Why do you need to divide the groups ?
 
D

DagoFlores

Hi, thank you for your idea. I found an option to do what I was
looking for:

public static List<Countries> GetCountries( List<Countries>
CountriesList, CountryProps Params, Countries myCountry ) {
List<Countries> myCountries = new List<Countries>();

switch (Params) {
case CountryProps.Continent:
myCountries = CountriesList.FindAll( delegate(
Countries myC ) { return myC.Continent == myCountry.Continent; } );
break;
...
default:
break;
}
}

This method returns a list of countries that has the same property than
the one of myCountry. Btw, I can get a list of countries that has the
same Continent than myCountry, and so on.

Cheers,
 

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

Similar Threads


Top