ASP NET MVC Error In Function
<p>This is my code. I got this sample from the Internet and I tried to modify it.</p>

<pre><code>    private void FillGridData()
    {
        //IQueryable&lt;SVC&gt; query = _customerService.GetQueryable();

        _dataContext = new dbServiceModelDataContext();
        var query = from m in _dataContext.SVCs
                    select m;

        query = AddQuerySearchCriteria(query, _grid.SearchForm);

        int totalRows = query.Count();
        _grid.Pager.Init(totalRows);

        if (totalRows == 0)
        {
            _grid.Data = new List&lt;SVC&gt;();
            return;
        }

        query = AddQuerySorting(query, _grid.Sorter);
        query = AddQueryPaging(query, _grid.Pager);

        List&lt;SVC&gt; customers = query.ToList(); //***ERROR IN HERE***//
        _grid.Data = customers;
    }
</code></pre>

<p>The error says "<strong>Cannot order by type 'System.Object'</strong>.", what is the matter?
Do you have solution for me?</p>

<p>This is The AddQuerySorting Method  <strong>THE PROBLEM IS IN HERE</strong> <br />
is there anything wrong about the code? :(</p>

<pre><code>    private IQueryable&lt;SVC&gt; AddQuerySorting(IQueryable&lt;SVC&gt; query, Sorter sorter)
    {
        if (String.IsNullOrEmpty(sorter.SortField))
            return query;

        //Used approach from http://www.singingeels.com/Articles/Self_Sorting_GridView_with_LINQ_Expression_Trees.aspx
        //instead of a long switch statement 
        var param = Expression.Parameter(typeof(SVC), "customer");
        var sortExpression = Expression.Lambda&lt;Func&lt;SVC, object&gt;&gt;
                                (Expression.Convert(Expression.Property(param, sorter.SortField), typeof(object)), param);

        if (sorter.SortDirection == SortDirection.Asc)
            query = query.OrderBy(sortExpression);
        else
            query = query.OrderByDescending(sortExpression);

        return query;
    }
</code></pre>

<p>here is AddQueryPaging Method</p>

<pre><code>    private IQueryable&lt;SVC&gt; AddQueryPaging(IQueryable&lt;SVC&gt; query, Pager pager)
    {
        if (pager.TotalPages == 0)
            return query;

        query = query.Skip((pager.CurrentPage - 1) * pager.PageSize)
                            .Take(pager.PageSize);
        return query;
    }
</code></pre>

<p>Sorter</p>

<pre><code>    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Web;

    namespace MvcGridSample.ViewModels.Shared
    {

        public enum SortDirection
        {
            Asc = 1,
            Desc = 2
        }


        public class Sorter
        {
            //Properties
            public string SortField { get; set; }
            public SortDirection SortDirection { get; set; }

            public Sorter()
            {
                this.SortDirection = SortDirection.Asc;
            }


            public Sorter(string sortField, SortDirection sortDirection)
            {
                Verify.Argument.IsNotEmpty(sortField, "sortField");
                Verify.Argument.IsNotEmpty(sortField, "sortDirection");

                this.SortField = sortField;
                this.SortDirection = sortDirection;
            }


            public void AddToQueryString(NameValueCollection queryString)
            {
                queryString["Sorter.SortField"] = this.SortField;
                queryString["Sorter.SortDirection"] = this.SortDirection.ToString();
            }
        }
    }
</code></pre>
