Best Way to Filter A Collection

Posts   
 
    
Kodiak
User
Posts: 92
Joined: 13-Apr-2009
# Posted on: 03-Jun-2009 09:54:55   

Hi Everyone,

Just wondering what would be the best approach to the following problem.

Products may have multiple Sizes. The Product -> Size data is held in a Product Size Entity.

Product Sizes can be deactivated.

Aim is to retrieve a list of all Sizes that do not currently exist in an Active Product Size.

I started off using the following

        'Retrieve all Active Sizes for the current Product
        Dim lProductSizeFilter As New PredicateExpression
        lProductSizeFilter.Add(HelperClasses.ProductSizeFields.FkProductId = CurrentEntity.PkProductId)
        lProductSizeFilter.AddWithAnd(HelperClasses.ProductSizeFields.Active = True)

        Dim sizesFilter As IPredicateExpression = New PredicateExpression()
        sizesFilter.Add(New FieldCompareSetPredicate(HelperClasses.SizeFields.PkSizeId, HelperClasses.ProductSizeFields.FkSizeId, SetOperator.In, lProductSizeFilter, True))
        sizesFilter.Add(New PredicateExpression(HelperClasses.SizeFields.Active = True))
        Dim lSortOrder As New SortExpression(New SortClause(HelperClasses.SizeFields.Name, SortOperator.Ascending))

        mAvailableSizes.GetMulti(sizesFilter, 0, lSortOrder)

But the problem is that this pulls back the results based on what is currently stored in the DB and not from the entities being used.

What's the best way this can be changed to give a list based on the current collection of Product Sizes in memory?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Jun-2009 10:03:47   

Instead of using a FieldCompareSetPredicate, use a FieldCompareRangePredicate and pass in a an array of FkSizeId values from the ProductSize collection in memory. And don't forget to set the negate parameter to true, because you want all sizes not in the passed range.

Kodiak
User
Posts: 92
Joined: 13-Apr-2009
# Posted on: 03-Jun-2009 10:37:18   

Thanks Walaa.

What would you recommend as the most efficient way of getting the IDs into an array?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Jun-2009 10:53:33   

Just loop over the collection, and manually collect these values.

arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 03-Jun-2009 16:45:11   

What would you recommend as the most efficient way of getting the IDs into an array?

Just loop over the collection, and manually collect these values.

or use linq to objects

int[] array = collection.Cast<Entity>.Select(x=>x.Id).ToArray();