This is a migrated thread and some comments may be shown as answers.

[Solved] Get property name from entity to use in custom sorting

1 Answer 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mattias
Top achievements
Rank 1
Mattias asked on 28 Apr 2010, 08:23 PM
Hi,
This is not exactly a telerik question but maybe you guys can help me out! :)
I have this code:
[GridAction] 
        public ActionResult Index(GridCommand command) 
        { 
            MenuRepository rep = new MenuRepository(); 
 
            List<System.Linq.Expressions.Expression<Func<Menu, object>>> expressions = new List<System.Linq.Expressions.Expression<Func<Menu, object>>>(); 
            List<System.Linq.Expressions.Expression<Func<Menu, object>>> expressionsDesc = new List<System.Linq.Expressions.Expression<Func<Menu, object>>>(); 
             
            //Apply sorting 
            foreach (SortDescriptor sortDescriptor in command.SortDescriptors) 
            { 
                if (sortDescriptor.SortDirection == ListSortDirection.Ascending) 
                { 
                    switch (sortDescriptor.Member) 
                    { 
                        case "Name"
                            expressions.Add(m => m.Name); 
                            break
                        default
                             expressions.Add(m => m.Name); 
                            break
                    } 
                } 
                else 
                { 
                    switch (sortDescriptor.Member) 
                    { 
                        case "Name"
                            expressionsDesc.Add(m => m.Name); 
                            break
                        default
                            expressions.Add(m => m.Name); 
                            break
                    } 
                } 
            } 
 
            int totalPages = 0; 
 
            IEnumerable<Menu> menus = rep.GetPagedMenus<Menu>(nullnull, expressions, expressionsDesc, command.Page, command.PageSize, out totalPages); 
 
            ViewData["total"] = totalPages; 
 
            return View(menus); 
        } 
It's the sorting part I'm wondering about.
If I have a lot of columns in my grid then I would need to add a huge number of cases in the switch.
Is it possible to do something like:
//Apply sorting 
foreach (SortDescriptor sortDescriptor in command.SortDescriptors) 
    if (sortDescriptor.SortDirection == ListSortDirection.Ascending) 
    { 
         expressions.Add(m => m.GetProperty(sortDescriptor.Member)); 
    } 
    else 
    { 
         expressionsDesc.Add(m => m.GetProperty(sortDescriptor.Member));    
    } 

Is it possible to add a GetProperty method somehow or is there another way to solve this?

Regards,
Mattias


1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 29 Apr 2010, 08:33 AM
Hi Mattias,

This is not possible however you can create an Expression and pass it to the OrderBy method. We have a class that you can use if you know the type of the property:

data = data.OrderBy(ExpressionBuilder.Expression<Order, string>(sortDescriptor.Member));

If the property type is not known you can use dynamic linq.
Regards,
Atanas Korchev,
the Telerik team
Tags
Grid
Asked by
Mattias
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or