Error on Custom sorting example

3 posts, 0 answers
  1. Kevin Hendriks
    Kevin Hendriks avatar
    56 posts
    Member since:
    Feb 2009

    Posted 28 Feb 2012 Link to this post

    Hello,

    I have implemented the custom sorting example http://www.telerik.com/help/wpf/gridview-sorting-custom.html 
    But using the following line of code results in an error that it cannot sort by System.Object
    employees = employees.OrderByDescending( employee => employee.GetType()
                                                                .GetProperty( (e.Column as GridViewDataColumn).GetDataMemberName() )
                                                                .GetValue( employee, null ) );
    So I think the example code is not working, but I do not seem to be able to figure out how to get it working.
    I am using this code on the server side so the code looks like this.

    IQueryable<Customer> custList = dbProPlan.Customers.Where(cust => !cust.IsDeleted);
                    if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Ascending)
                    {
                        custList = custList.OrderBy(cust => cust.GetType().GetProperty(propertyName).GetValue(cust, null));
                    }
                    else if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Ascending)
                    {
                        custList = custList.OrderByDescending(cust => cust.GetType().GetProperty(propertyName).GetValue(cust, null));
                    } 


                    numberOfItems = custList.Count();
                    double pages = numberOfItems / aPageSize;
                    int numberOfPages = (Math.Ceiling(pages) >= 1 ? (int)Math.Ceiling(pages) : 1);
                    aPageNumber = (numberOfPages < aPageNumber ? numberOfPages - 1 : aPageNumber);


                    IEnumerable<Customer> customerList = custList.Skip(aPageNumber * aPageSize).Take(aPageSize);
                    dbProPlan.Refresh(RefreshMode.OverwriteCurrentValues, customerList);
                    return customerList;

    What do I need to change to get this working properly.
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 02 Mar 2012 Link to this post

    Hello,

     Can you post the stack trace? The grid version will be helpful as well. 

    Greetings,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Kevin Hendriks
    Kevin Hendriks avatar
    56 posts
    Member since:
    Feb 2009

    Posted 09 Mar 2012 Link to this post

    I have solved the orror by using the following code
    public static Expression<Func<T, object>> OrderExpression<T>(string propertyName)
            {
                // First we define the parameter that we are going to use in our OrderBy clause. This is the same as "(person =>"
                var param = Expression.Parameter(typeof(T), "item");
     
                // Now we'll make our lambda function that returns the property by it's name.
                var mySortExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, propertyName), param);
                return mySortExpression;
            }
     
    public IEnumerable<Customer> GetAllCustomers(int aPageNumber, int aPageSize, out int numberOfItems, string propertyName, SortingState sortingState)
            {
                numberOfItems = 0;
                try
                {
                    IQueryable<Customer> query = dbProPlan.Customers.Where(cust => !cust.IsDeleted);
                    if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Ascending)
                    {
                        query = query.OrderBy(OrderExpression<Customer>(propertyName));
                    }
                    else if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Descending)
                    {
                        query = query.OrderByDescending(OrderExpression<Customer>(propertyName));
                    }
                    numberOfItems = query.Count();
                    double pages = numberOfItems / aPageSize;
                    int numberOfPages = (Math.Ceiling(pages) >= 1 ? (int)Math.Ceiling(pages) : 1);
                    aPageNumber = (numberOfPages < aPageNumber ? numberOfPages - 1 : aPageNumber);
     
                    IEnumerable<Customer> customerList = query.Skip(aPageNumber * aPageSize).Take(aPageSize);
                    dbProPlan.Refresh(RefreshMode.OverwriteCurrentValues, customerList);
                    return customerList;
                }
                catch (Exception e)
                {
                    LogWriter.WriteLine(LogTypes.Error, string.Format("GetAllCustomers() failed; {0}", e.Message), e.StackTrace);
                    return null;
                }
            }
Back to Top