ObjectListView filtered by child objects

ObjectListView filtered by child objects

Old forum URL: forums.lhotka.net/forums/t/3541.aspx


SonOfPirate posted on Friday, September 14, 2007

I have a list of objects.  Each object contains a list of the categories to which it belongs.  (Each object can exist in more than one category.)  Each child Category object has a Name property that indicates the name of that category.

In my application, I present the user with this list of objects bound to a simple list control.  This is accomplished using a "default" view with pre-defined sorting and no filtering.  Via a drop-down list, the user can select which category they would like to filter the list by.  Ordinarily, if there was a one-to-many relationship between the category and my objects, this would be simple and I'd just create a new ObjectListView filtered on "Category = 'x'".  However, because there is no one Category property or value, this won't work.

I tried using "Child.Name = 'x'" as my filter expression but that threw an exception from ADO.NET saying that it didn't recognize Child.  I also tried having my Category collection return a string containing a comma-delimited list of the names so I could use "'x' IN Categories" where Categories is the property name on my object - but it didn't like that either.

So, how do I accomplish this?  Essentially I need a way to filter the list based on whether the object contains a child Category object with the specified name - or the name is in a list exposed by the object.  Can this be done with the ObjectListView?

Any suggestions?

Thx

 

KKoteles replied on Friday, September 14, 2007

SonOfPirate,

What about adding another "Get" method to your list object?  So where you now have ObjectList.Get(), you would also create an ObjectList.GetByCategory() (or more than one if you have different parameter signatures). The Data_Portal_Fetch for that particular method would call its own specific stored procedure for the objects to be returned.  All the real work is done in the stored procedure (where you would return the object if ANY category associated with it matched...).  The idea is that you are not really "filtering" a returned recordset, but rather controlling what is being returned in the recordset in the first place (yes, you will need to be able to address parameters being passed in the fetch).  So:

if (condition)  // You need to get list by Category

{

   DisplayList(ObjectList.GetByCategory());

}

else

{

   DisplayList(ObjectList.Get());

}

 

private void DisplayList(ObjectList list)

{

   Csla.SortedBindingList<ObjectInfo> sortedList = new Csla.SortedBindingList<ObjectInfo>(list);

   sortedList.ApplySort("Name", ListSortDirection.Ascending);

   this.ObjectListBindingSource.DataSource = sortedList;

}

 

Just a thought, 

Ken K

SonOfPirate replied on Monday, September 17, 2007

Yea, I guess I should have included in my original post that the app is "document-centric" in nature meaning that we have to operate as if all of our objects exist ONLY in memory.  Therefore, instantiating different lists using SQL to do the filtering is not an option.

Essentially, the user creates a new "document" and begins adding items.  Until they explicitly save the "document", nothing is persisted in the database.  As a result, all of the filtering required to support multiple views in the UI must be done directly on the business collections.

Besides that, making round-trips everytime they want to VIEW that data differently is undesirable and highly unproductive (imo).  In addition, we'd be consuming more and more memory as we'd actually have multiple instances of the same object in memory - another key to why we are using the ObjectListView approach.

Again, the issue is that we want to filter our list based on whether an item contains a particular value or object in its child collection.  It's the many-to-many relationship that exists between our objects and each "Category" that makes this different from the standard pattern.

Thx.

 

 

Brian Criswell replied on Tuesday, November 20, 2007

Hi, the ObjectListView is still supported by its author, although it may seem as though I have fallen off of the face of the earth.  A combination of a move from Oxford to San Diego, a career change to the construction industry, 45 minute commutes, an apartment that is still not unpacked and a six month old baby can do that to you. Wink [;)]  Last night was the first time I had unpacked and turned on my computer since the end of July.

I think that it might work if you tried something like
"Categories like %" + categoryName + "%"

against your comma delimited list.  I realize that this is really old and you may have moved on from this, but that is my best guess.

By the way, to anyone looking for support on the ObjectListView, send me a PM.  That will send me an email, which I should be more responsive to.

Copyright (c) Marimer LLC