Home All Groups Group Topic Archive Search About

Working with FindAll() and predicates.

Author
15 Dec 2006 3:16 AM
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,

Author
15 Dec 2006 3:55 AM
Jon Shemitz
DagoFlores wrote:

> 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);
      } );
  }

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Are all your drivers up to date? click for free checkup

Author
15 Dec 2006 5:18 PM
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,
Author
15 Dec 2006 8:00 AM
Pete Sambras
DagoFlores wrote:
Show quoteHide quote
> 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 ?

Bookmark and Share