Filter Collection

S

Stanley

Hello all,
I have a custom collection that I am using to fill a DropDown list that
I need to filter out items based on another dropdown list. My problem is
the actual filter in the collection. I can add a filter sub to the
collection code but I am not sure how to find which property to use. Say
if my custom object is like this:
Person
ID
FirstName
LastName

And then I create a collection of Persons (using CollectionBase) how
would I be able to tell my filter sub which field I want to filter by
and how would I not lose all the records in the collection as I do not
want to have to hit the db again to get back the rest of the data that
was filtered out? Also this collection is being used for both ASP.NET
and Winforms so I need to do this in the collection not on the GUI side.

-Stanley
 
K

Karl Seguin

Well, ur 2nd dilemma is simple. In your function, create a new collection
and populate it with matches. There are a couple ways to solve your first
problem, but I'll suggest the Criteria approach used in the CSLA.Net
(http://www.lhotka.net/ArticleIndex.aspx?area=CSLA .NET)

I'd create a Criteria object which can have the properties you want to
filter on...something like:

public class PersonCriteria
{
private int id;
private string firstName;
priate string lastName;

//appropriate constructors and properties here
}


I'd then do:

PersonCriteria pc = new PersonCriteria(0, null, "blah");

and I'd add the following function to the collection

public PersonCollection Find(PersonCriteria pc)
{
PersonCollection foundCollection = new PersonCollection();
foreach (Person p in this.List)
{
if ((pc.Id = 0 || pc.Id = p.Id) && (pc.FirstName == null ||
pc.FirstName == p.FirstName) && ...)
{
foundCollection.Add(p);
}
}
return foundCollection;
}

Also, check out:
http://weblogs.asp.net/plip/articles/111127.aspx

Karl
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top