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...