|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Working with FindAll() and predicates.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, DagoFlores wrote:
> But, what about if I want to work with another type than String or FindAll takes a Predicate<T> - a delegate to a method that takes a> 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. 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 Fwiw, you should take a look at the String.EndsWith method - there is> constants ("saurus") by thisMatch ? > > private static bool EndsWithSaurus(String s) > { > if ((s.Length > 5) && > (s.Substring(s.Length - 6).ToLower() == "saurus")) > { a overload that can be case-insensitive. > private static bool EndsWithSaurus(String s, String thisMatch) I'm probably not understanding your question, because I see no link> { > if ((s.Length > 5) && > (s.Substring(s.Length - 6).ToLower() == thisMatch)) > { 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); } ); } 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, DagoFlores wrote:
Show quoteHide quote > Hi, let's take a look to this code that we already know.: Why do you need to divide the groups ?> > 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,
Other interesting topics
sending msg to message loop of background thread
how to cast an int to a string? Payroll model. Making a beep Stopping Automation ArrayList of arrays csharp skipping lines of code after a call...? Data access layer generator? When to use the GAC? Crystal Reports for VS.Net 2003 (Export Margin Problem) |
|||||||||||||||||||||||