In one of my project I was in need of the List.FindAll method in . Net 2.0. I was unable to find any useful enough details about this, even though I did a good amount of searching in google. After a while, I ended up in the following code . My need was to find the exact object of my custom class in the collection given, matching my criteria. The List.FindAll accepts a predicate as parameter in which we can specify our own criteria. Predicate (System.predicate) is a new facility introduced in .Net 2.0 which enables us to mention our custom criteria.
Here is the code:
public class MYList:List<MyCustomCLASS>
{
private int currentindex;
///
/// Constructor
///
public MYList ()
{}
public List<> GetItemsContaining(double text)
{
return this.FindAll(delegate(MyCustomCLASS myobj) {return myobj.ItemId == text; });
}
Here the GetItemsContaining uses the base class‘s FindAll method to check and return a list of objects matching the criteria. We can also use List.Find (if you want a single object to be returned from the colllection) and List.ConvertAll (Which converts all objects in the collection to specified objects) in the same logic.
And the MyCustomCLASS is nothing but a normal class which contains an ItemId as one of it's property and ofcourse... ya it contaims some other properties too which holds the buisness specific data records
No comments:
Post a Comment