0

This is my code. I got this sample from the Internet and I tried to modify it.

    private void FillGridData()
    {
        //IQueryable<SVC> 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<SVC>();
            return;
        }

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

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

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

This is The AddQuerySorting Method THE PROBLEM IS IN HERE
is there anything wrong about the code? :(

    private IQueryable<SVC> AddQuerySorting(IQueryable<SVC> 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<Func<SVC, object>>
                                (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;
    }

here is AddQueryPaging Method

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

        query = query.Skip((pager.CurrentPage - 1) * pager.PageSize)
                            .Take(pager.PageSize);
        return query;
    }

Sorter

    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();
            }
        }
    }
RahdixC9
  • 3
  • 5
  • You should try to accept the answers given to questions that were helpfull. It is the correct way to go about things, and gives others incentive to help you... – Adriaan Stander Apr 26 '10 at 04:10
  • 1
    what is inside of AddQueryPaging function? – zerkms Apr 26 '10 at 04:14
  • AddQueryPaging function is a function that make data displayed in the grid consisting of several pages like this < << pages > >> – RahdixC9 Apr 26 '10 at 05:02
  • You are showing two methods (`AddQuerySorting` , `AddQueryPaging`) that we can't possibly know about. There is not enough information here for *anyone* to answer this. The error is *probably* inside `AddQuerySorting`, but is *showing* as `ToList()` due to the deferred execution model of LINQ. You *must* show `AddQuerySorting` (and probably `AddQueryPaging`) for anyone to help. – Marc Gravell Apr 26 '10 at 06:49
  • thx gravell... i'll update my question soon.. – RahdixC9 Apr 26 '10 at 07:17
  • Ok, the problem is probably that your sortExpression is wrong - but you don't see the problem 'til you attempt to evaluate the linq expression where you have the comment. You need to see what you have for sortExpression evaluates to in "AddQuerySorting" – Murph Apr 26 '10 at 08:17
  • yes..you're right Murph..when i comment out the AddQuerySorting method, it's all works..now I'm tracking the location of errors in AddQuerySorting. thx for your assistance.. – RahdixC9 Apr 26 '10 at 09:04
  • i cannot find the solution.. what's wrong with the sortExpression??somebody please help... – RahdixC9 Apr 27 '10 at 04:06

1 Answers1

0

Look at the line:

var sortExpression = Expression.Lambda<Func<SVC, object>>

That should immediately leap out as the cause. The generated Expression must be suitable typed. This type of metaprogramming often involves either using the non-generic API, or using reflection to create the correct type on the fly. Fortunately, a suitable example is here. You should be able to use that approach re MakeGenericMethod (or simpler: just use the code "as is" from inside AddQuerySorting).

Community
  • 1
  • 1
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • Mr Gravell...i still confuse about manipulating the code like you said I've updated my question by add Sorter unction..any solution?? – RahdixC9 Apr 27 '10 at 05:58