Sort using the values of multiple fields?

Posts   
 
    
Kristina avatar
Kristina
User
Posts: 2
Joined: 30-May-2007
# Posted on: 30-May-2007 07:37:53   

Hi there,

I need to sort an EntityCollection by a custom value, which is the sum of two fields in the entity.

For example, I have a EntityCollection of Customer entities, and the Customer entity has two fields, OrdersPaid and OrdersUnpaid. I want to sort the customers by their total number of orders, so I need to add the values of the two fields THEN sort the collection.

Can this be done? I am using LLBLGen Pro 2.0.

Thanks very much!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-May-2007 09:35:44   

First it would be easier if you manually created a custom property inside your Entity class, to return the addition of the OrdersPaid and OrdersUnpaid fields. (make sure you define the new property inside a User Code region "CustomEntityCode", so it won't get overwritten when you re-generate the code)

Then, you should use an EntityView to sort the EntityCollection on the client-side. Using the new entity property as follows:


// Adapter C# example, use EntityView instead of EntityView2 for SelfServicing

EntityView2<CustomerEntity> customerView = new EntityView2<CustomerEntity>(customers);
ISortExpression sorter = new SortExpression(new EntityProperty("TotalOrders") | SortOperator.Descending);
customerView.Sorter = sorter;
Kristina avatar
Kristina
User
Posts: 2
Joined: 30-May-2007
# Posted on: 04-Jun-2007 02:12:59   

Hi there,

Thanks very much for the prompt reply. I put the snippet in my code but the build is failing because of this error:

The type must be convertible to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase' in order to use it as parameter 'TEntity' in the generic type or method 'SD.LLBLGen.Pro.ORMSupportClasses.EntityView<TEntity>'

Not sure what to do about this, sorry!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Jun-2007 08:18:30   

Please post what you have tried (code snippet).