By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,081 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,081 IT Pros & Developers. It's quick & easy.

Filter Collection

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.a...ea=CSLA%20.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

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Stanley" <st*****@glass-images.com> wrote in message
news:Os**************@tk2msftngp13.phx.gbl...
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

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.