Geeks With Blogs
Dheeman Dutta Just Another Blog.....
In one of my previous article (October 2006) on this topic , I’ve shown how to use the Exists() method of a list in order to find an object in the list that matched a certain value. However there I also mentioned about one disadvantage
 
            “the value to be checked needs to be more or less predetermined (hardcoded) as the delegate method does not take any other parameters.”
 
Therefor it meant that an Exists method can only find an object in a lIst , if it contained on perdetermined serach condition, it will however fail to find if the search condition changes . And I found no good way of getting around this limitiaton then. Here I’ll be presenting a way to perform a cutomised find, such that the search criteria need not be hard-coded. (ref method CheckIfExists() in the October2006 article) . If you have ever used any method that a .net generic list exposes and has a Predicate<> as a parameter (for e.g. Exists()), the above mentioned disadvantage is there. The predictate is a delegate declared in the .Net framework. Because of this this delegate , your methods need to match the signature of this delegate in order to be used in a method like Exists(), which takes . As a result you won’t be able to add any other parameter to your method , like customsearchCondition in your method as this voilates the signature of the Predicate<> delegate.
So I first overloaded the Predicate<> delegate to have my own Signature
 
public delegate bool Predicate<T>(T obj, string customsearchCondition);
 
Once this is done , I created a class of my own , ListBase<T> which inherits from List<T> , so that I can use all the existing facilitis that a .net List<> provides without adding any more code.
 
public class ListBase<T>:List<T>
 
Now I add the following method to my class
 

 

 private bool TryFindFirstWhere<T>(IEnumerable<T> collection, Predicate<T> predicate, out T foundItem, RequestId matchVal)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            if (predicate == null)
                throw new ArgumentNullException("predicate");

            foreach (T item in collection)
            {
                if (predicate(item,matchVal))
                {
                    foundItem = item;
                    return true;
                }
            }

            // didn't find any item that matches.
            foundItem = default(T);
            return false;
        }

 
 
This method ensures that I have to pass my search condition along with the Predicate<> and returns a boolean value if the object is found in the list. In order to actually get the object from the list, I also add one out parameter, which will assigned the object once it is found. If the object is not found, I’m returning the default value of the data type.
Any better suggestions would always be appreciated...........
Cheers
Posted on Wednesday, July 16, 2008 8:56 PM | Back to top


Comments on this post: Adding Custom Search Conditions in your List.Exists() method.

# re: Adding Custom Search Conditions in your List.Exists() method.
Requesting Gravatar...
Good work. Thanks.
Left by Harry Vu on Aug 15, 2008 9:56 AM

# re: Adding Custom Search Conditions in your List.Exists() method.
Requesting Gravatar...
It sounds good, but your code snippet doesn't work for me. Could you post the full souce code in this article? Thanks.
Left by Richard on Aug 26, 2008 10:22 AM

# re: Adding Custom Search Conditions in your List.Exists() method.
Requesting Gravatar...
Thanks for dropping by. I can post my code by this weekend , I guess.
Left by Dheeeman on Aug 28, 2008 5:35 AM

Your comment:
 (will show your gravatar)


Copyright © Dheeman Dutta | Powered by: GeeksWithBlogs.net