0

Possible Duplicate:
How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

I am using LINQ orderby statement for selecting the parent table with childtable. Now I want to apply orderby in all the fields including parent table and child table. How can i achieve this using dynamic column name. Please help me... This is the code i used here.

result = (from p in StudentDB.Student.Include("Student_addr")
                                  where (p.full_nm.Contains(searchCriteria.Name))
                                  select p);

Now I am calling an extension method for applying orderby ...

var stuType = typeof(Student);
                    var addressType = typeof(Student_addr);
                    list = result.Order<Student>(stuType , addressType ,searchCriteria.sortColumn, searchCriteria.sortCondition);

return  list.Take(searchCriteria.reccount).ToList();

Extension method....

public static IQueryable<T> Order<T>(this IQueryable<T> items, Type stuType , Type addressType, string sortColumn, string sortOrder)
        {
            var typeOfT = addressType;
            var parameter = Expression.Parameter(typeOfT, "parameter");
            var propertyType = typeOfT.GetProperty(sortColumn).PropertyType;
            var propertyAccess = Expression.PropertyOrField(parameter, sortColumn);
            var orderExpression = Expression.Lambda(propertyAccess, parameter);

            Expression  expression =null;
            if (sortOrder == "Desc")
            {
                expression=Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { stuType , propertyType }, items.Expression, Expression.Quote(orderExpression));
            }
            else
            {
                expression = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { stuType , propertyType }, items.Expression, Expression.Quote(orderExpression));
            }
            return items.Provider.CreateQuery<T>(expression);
        } 

While I am running on the child table column name i got error as follos:

No generic method 'OrderBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

Thanks in advance...

Community
  • 1
  • 1
felix Antony
  • 1,450
  • 14
  • 28
  • 1
    Have you tried casting as/using typeof IQueryable instead? – Davio Dec 11 '12 at 14:44
  • No I havenot tried that. Can you please explain how can i casting it using 'using'? – felix Antony Dec 11 '12 at 14:51
  • `expression=Expression.Call(typeof(IQueryable), "OrderByDescending", new Type[] { stuType , propertyType }, items.Expression, Expression.Quote(orderExpression));` – Davio Dec 11 '12 at 14:52
  • Yes I tried this code also at that time I got the error as: "No generic method 'OrderBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. " – felix Antony Dec 11 '12 at 14:57
  • While I tried with IQueryable I got the error as follows: "No method 'OrderBy' exists on type 'System.Linq.IQueryable'." – felix Antony Dec 11 '12 at 14:58
  • Expression.Lambda has a generic brother, maybe you need to use that or some other generic siblings? – Davio Dec 11 '12 at 15:04
  • could you please provide me any link for reference...? – felix Antony Dec 11 '12 at 15:07
  • http://stackoverflow.com/questions/2850265/calling-a-generic-method-using-lambda-expressions-and-a-type-only-known-at-runt – Davio Dec 11 '12 at 15:10
  • Thanks I am goingtrrough the posts now... – felix Antony Dec 11 '12 at 15:14
  • sorry to say that i have not yet got any solution... Please help me... – felix Antony Dec 12 '12 at 02:50

0 Answers0