Filtering in-memory collections

Posts   
 
    
cjmos
User
Posts: 15
Joined: 04-May-2007
# Posted on: 24-Jul-2007 15:56:24   

Hi all,

Sorry if this is mentioned somewhere in the manual and I've missed it.

I have a CartEntity (represents a shopping cart) which has a collection 'CartDetailCollection'. CartDetailEntity is at the top of a TargetPerEntity hierarchy with two sub-types ProductDetailEntity and ContentDetailEntity.

When I load my CartEntity into memory (using prefetch paths to get the associated CartDetailCollection), I would like to be able to get collections of CartDetailEntities based on there type, for example, get all the ProductDetailEntities from my in-memory cart, without another call to the db.

The reason why I don't want to use 'Filtering on entity type' is because I am doing a lot of work to the CartEntity.CartDetailCollection in-memory so I want to avoid having to save the changes to the database and then re-fetch them using GetEntityTypeFilter() as this seems like an unneccessary call to the db.

Here's the method I'm using so far - Is there a better way?

        public static CartDetailCollection GetContentItemsFromCart(CartEntity cart)
        {
            CartDetailCollection contentItems = new CartDetailCollection();
            foreach (CartDetailEntity cartDetailEntity in cart.CartDetail)
            {
                if(cartDetailEntity is ContentDetailEntity)
                {
                    contentItems.Add(cartDetailEntity);
                }
            }
            return contentItems;
        }

Thanks.

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 24-Jul-2007 16:15:12   

Hi,

you can apply the FindMatches(IPredicate) method on your entitycollection and create a predicate with the GetEntityTypeFilter(). So you don't have to re-fetch your collection.

siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 24-Jul-2007 16:24:18   

Thanks for that,

That looks like a great method for complicated filters, I'm just wondering if this method is any faster than the method I'm using now or does it pretty much do the dame thing under the covers?

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 24-Jul-2007 16:51:03   

siegemos wrote:

Thanks for that,

That looks like a great method for complicated filters, I'm just wondering if this method is any faster than the method I'm using now or does it pretty much do the dame thing under the covers?

What method do you use now?

siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 24-Jul-2007 16:53:45   

The code in my first post

        public static CartDetailCollection GetContentItemsFromCart(CartEntity cart)
        {
            CartDetailCollection contentItems = new CartDetailCollection();
            foreach (CartDetailEntity cartDetailEntity in cart.CartDetail)
            {
                if(cartDetailEntity is ContentDetailEntity)
                {
                    contentItems.Add(cartDetailEntity);
                }
            }
            return contentItems;
        }
jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 24-Jul-2007 16:59:51   

I'm sorry but It was not the same user name, so I don't know it was your code wink

It's a more generic code but it does the same(check for each entities if predicate match or not).

siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 24-Jul-2007 17:09:56   

Oh yeh sorry simple_smile I changed browsers, must have logged me in on my old profile (which I forgot I had).

Thanks for the help.