Converting an SQL sort expression to an LLBLGen SortExpression

Posts   
 
    
Marco avatar
Marco
User
Posts: 13
Joined: 27-Jul-2007
# Posted on: 27-Jul-2007 10:48:27   

Hi,

We are using the ComponentArt Grid in a project (I read that you don't fancy this component that much, Frans wink ) and all is working fine except for the sorting. When the user presses the column header to sort a column, the grid's Sort property is filled with the SQL sort expression needed to do that sort (like "Description ASC") and I am trying to convert that into an LLBLGen SortExpression. And of course I want to make that as generic as possible. Below is the code I have so far. This works fine, if the column that is used in the grid is from the same entity type that I have passed to the GetSortExpression function, but I can't figure out how to do the same if the column is from a 'joined entity'. The sqlSortExpression then is something like "CreatedByEmployee.FirstLastName ASC". So, in that case I first have to figure out what entity type CreatedByEmployee is and then set the current entity variable to that type. Now my question is: is that possible? I have set the entity variable to the correct entity type manually (new EmployeeEntity()) and then the privided SQL sort expression works fine too. Anyway, hope I am being clear. simple_smile


        /// <summary>
        /// Returns an LLBLGen SortExpression based on a SQL sort string (sorted on 1 field)
        /// </summary>
        /// <param name="entity">Example: QuestionnaireEntity</param>
        /// <param name="sqlSortExpression">Example: Description ASC</param>
        /// <returns>The LLBLGen SortExpression</returns>
        public static SortExpression GetSortExpression(EntityBase2 entity, string sqlSortExpression)
        {
            SortExpression sortExpression = new SortExpression();
            if (!string.IsNullOrEmpty(sqlSortExpression))
            {
                Array sqlSortExpressionArray = sqlSortExpression.Split(Char.Parse(" "));

                // Set the sort order
                string sortOrder = string.Empty;
                sortOrder = sqlSortExpressionArray.GetValue(1).ToString();
                SortOperator sortOperator = new SortOperator();
                if (sortOrder.Equals("ASC"))
                {
                    sortOperator = SortOperator.Ascending;
                }
                else
                {
                    sortOperator = SortOperator.Descending;
                }

                // Set the sort field
                string sortField = string.Empty;
                sortField = sqlSortExpressionArray.GetValue(0).ToString();

                // ------------ not generic yet --------------
                if (sortField.Contains("."))
                {
                    // CreatedByEmployee.FirstLastName ASC
                    Array sortFieldArray = sortField.Split(Char.Parse("."));
                    // Walk through the entities
                    sortField = sortFieldArray.GetValue(sortFieldArray.Length-1).ToString();
                    for (int i = 0; i < sortFieldArray.Length-1; i++)
                    {
                        string fieldName = sortFieldArray.GetValue(i).ToString();
                        //foreach (EntityBase2 tempEntity in EntityClasses)
                        //{
                        //  try
                        //  {
                        //      entity.SetRelatedEntity(entity, fieldName);
                        //  }
                        //  catch (exception e)
                        //  {
                        //  }
                        //}
                        entity = new EmployeeEntity();
                    }
                }
                // ------------ not generic yet --------------

                SortClause sortClause = new SortClause(GetEntityField(sortField, entity.Fields), null, sortOperator);
                sortExpression.Add(sortClause);
            }
            return sortExpression;
        }

        private static IEntityField2 GetEntityField(string fieldName, IEntityFields2 fields)
        {
            IEntityField2 entityField2 = null;
            foreach (IEntityField2 tempEntityField2 in fields)
            {
                if (tempEntityField2.Name.Equals(fieldName))
                {
                    entityField2 = tempEntityField2;
                    break;
                }
            }
            return entityField2;
        }

I am using LLBLGen Pro 2 final by the way (with lots of joy).

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Jul-2007 11:16:12   

First let me get the following clear: Is this a windows or a web application? Are you using bindingSource / LLBLGenProDataSource?

Marco avatar
Marco
User
Posts: 13
Joined: 27-Jul-2007
# Posted on: 27-Jul-2007 11:21:41   

Thanks for the quick reply.

It's a web application and I am databinding the EntityCollection directly to the Grid webcontrol (so I guess LLBLGenProDataSource?).

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Jul-2007 11:27:12   

It's a web application and I am databinding the EntityCollection directly to the Grid webcontrol (so I guess LLBLGenProDataSource?).

If you bind the EntityCollection directly, then you are not using an LLBLGenProDataSource object. Please try to use an LLBLGenProDataSource, and to sort on a field of a related entity, - Map the related entity field to the entity being fetched as a "field on related field" - Set the SortMode to client (Client-Side). And sorting will be done on the EntityCollection view. (without going back to the database).

Marco avatar
Marco
User
Posts: 13
Joined: 27-Jul-2007
# Posted on: 27-Jul-2007 11:43:18   

I'll give that a try. Thanks.

Marco avatar
Marco
User
Posts: 13
Joined: 27-Jul-2007
# Posted on: 30-Jul-2007 15:48:54   

I have been trying to bind the ComponentArt Grid to an LLBLGenProDataSource2 object without any luck. Found on the LLBLGen Pro forums that ComponentArt apparently doesn't support LLBLGenProDataSource(2) components:

Otis wrote:

What!?, they don't support LLBLGenProDataSource(2) components?

disappointed (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=2010&HighLight=1) And found some people on the ComponentArt forums with the same problems. I did manage to bind the LLBLGenProDataSource2 to the default ASP.NET Grid component without any troubles though, so at least I learned something (didn't even know that the LLBLGenProDataSource components existed... flushed ).

Anyway... still would like to use the ComponentArt Grid. Thinking of passing an array with the needed Entity types to the GetSortExpression function now... Or is there something else I can use?

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 30-Jul-2007 18:32:18   

Hi,

I'm sorry but I don't see another solution...

But it is really amazing ComponentArt Grid don't support *Datasource controls...

Marco avatar
Marco
User
Posts: 13
Joined: 27-Jul-2007
# Posted on: 31-Jul-2007 09:11:15   

Aurelien wrote:

Hi, I'm sorry but I don't see another solution...

No problem.

Aurelien wrote:

But it is really amazing ComponentArt Grid don't support *Datasource controls...

Yeah, too bad. disappointed