Print Page | Close Window

OrderBy

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1821
Printed Date: 23-Apr-2025 at 1:12pm


Topic: OrderBy
Posted By: evanian
Subject: OrderBy
Date Posted: 18-May-2010 at 9:17am
Is it possible to OrderBy a spanned column in an RdbQuery?  What I would like to do is this:

            // Query
            _query = new RdbQuery(typeof(AS400Client));

            // Where clause
            _query.AddClause(    AS400Client.FirstNameEntityColumn,
                                EntityQueryOp.EQ,
                                "David");

            // Add span to other table
            _query.AddSpan(EntityRelations.AS400Workplace_AS400Client);

            // Add orderby on column in *related* table
            EntityOrderBy orderBy = new EntityOrderBy(AS400Workplace.DescriptionEntityColumn, ListSortDirection.Descending);

            _query.ClearOrderBy();           
            _query.AddOrderBy(orderBy);
           
            // Get the clients
            PersistenceManager pm = PersistenceManager.DefaultManager;
            _clients = pm.GetEntities<AS400Client>(_query);  


Of course, I get an error:  "Cannot find column Description."   Is there any way of doing this?

(The reason why I'm not simply defining the query as being of the AS400Workplace type is that I want, potentially, to order by columns in either table, dynamically, depending on user input).



Replies:
Posted By: IdeaBlade
Date Posted: 19-May-2010 at 5:06pm
You can't order by a spanned column because the EntityList returned by the query only contains the primary Entity type (AS400Client in your case).  You're trying to sort a collection of AS400Client entities on a column that doesn't exist on the AS400Client entity.

The effect of the span is simply to load the AS400Workplace into the cache as a byproduct of your query for AS400Clients.

If you want an entity that combines the columns of AS400Clients and AS400Workplace, you might want to create a view in your database and then base a new entity type on that.  However, that tends to become problematic if you need to change and save the retrieved entities.

You could probably write an IComparer<T> that would sort AS400Clients by a column on the related AS400Workplace (assuming that's a 1-1 relationship). See the example "Reusable MultiProperty Sorter" (and specifically the file MultiPropertyComparer.cs) under the BindableList topic in the 200 Intermediate Learning Units for an example of a custom IComparer<T>



Posted By: evanian
Date Posted: 20-May-2010 at 6:08am
Helpful ideas, thanks!



Print Page | Close Window