In-Memory Sorting of EntityCollection

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 06-Nov-2008 23:25:46   

I'm sure this information exists on these forums, but I have searched and have been unable to find an answer.

Is it possible to do an in-memory sort of an EntityCollection using a field on a related entity? The field is not mapped as a related field if that makes a difference.

So assume I have a collection of orders and want to sort them by the customer's CustomerCreatedDate field. I know how to do this on the database side, but not in-memory.

(Adapter 2.6.8.903)

Thanks,

Phil

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Nov-2008 03:29:43   

Hi Phil,

I can think in these ways:

Using MemberPredicate and ENtityView2

IPredicateExpression filter = new PredicateExpression();
MemberPredicate relatedFilter = new MemberPredicate(OrderEntity.MemberNames.Customer, MemberOperator.Any, (CustomerFields.CustomerId == "ALFKI"));          
filter.Add(relatedFilter);

EntityView2<OrderEntity> filteredOrders = new EntityView2<OrderEntity>(orders, filter);

Using Entitviews with a custom property

// __LLBLGENPRO_USER_CODE_REGION_START CustomEntityCode
public string CustomerName
{
    get
    {
        string toReturn = string.Empty;
        if (Customer != null)
        {
            toReturn = Customer.CompanyName;
        }
        return toReturn;
    }
}
// __LLBLGENPRO_USER_CODE_REGION_END
IPredicateExpression filter = new PredicateExpression();
filter.AddWithAnd(new EntityProperty("CustomerName") % "A%" );
EntityView2<OrderEntity> filteredOrders = new EntityView2<OrderEntity>(orders, filter);

Using Linq2Objects (.Net 3.5)

var filteredOrders = from o in orders
                     where o.Customer.CompanyName.StartsWith("A")
                     select o;
David Elizondo | LLBLGen Support Team
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 07-Nov-2008 04:30:40   

Hey David,

I was asking about sorting, not filtering. simple_smile

Or do they work similarly?

Thanks,

Phil

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Nov-2008 12:40:19   

Using Linq2Objects, and assuming for example you have fetched orders collection along with the related customer entity, then you can in-memory sort the orders collection on a field inside the customer entity as follows:

            var q = from o in orders
                     orderby o.Customers.PostalCode
                     select o; 

Then the new sorted collection can be found using q.ToList();

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 07-Nov-2008 15:25:23   

Sorry--I should have mentioned that Linq is not an option, as we are still on Visual Studio 2005 and .net 2.0/3.0.

Is there a non-linq method?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Nov-2008 18:44:49   

In that case, this is the way:

// __LLBLGENPRO_USER_CODE_REGION_START CustomEntityCode
public string CustomerName
{
    get
    {
        string toReturn = string.Empty;
        if (Customer != null)
        {
            toReturn = Customer.CompanyName;
        }
        return toReturn;
    }
}
// __LLBLGENPRO_USER_CODE_REGION_END
SortExpression sorter = new SortExpression();
sorter.Add(new SortClause(new EntityProperty("CustomerName"), null, SortOperator.Ascending));           
EntityView2<OrderEntity> orderedOrders = new EntityView2<OrderEntity>(orders, sorter);
David Elizondo | LLBLGen Support Team