none
linq orderby is not working with dynamic value on child table RRS feed

  • General discussion

  • Hi all,

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

    • Moved by Mike FengModerator Wednesday, December 12, 2012 2:27 AM (From:.NET Base Class Library)
    Tuesday, December 11, 2012 2:27 PM

All replies

  • Hi Felix,

    Welcome to the MSDN forum.

    I have not tested it, but please check this:

            public static IQueryable<T> Order<T>(this IQueryable<T> items, Type stuType, Type addressType, string sortColumn, string sortOrder)
            {
                var typeOfT = stuType;
                var parameter = Expression.Parameter(typeOfT, "p");
                var propertyType = typeOfT.GetProperty("Company").PropertyType;//Navigation property
                var parameter2 = Expression.Parameter(propertyType, "p");
                var propertyAccess2 = Expression.Property(parameter2, "Name");//Property of child 
                var orderExpression = Expression.Lambda(propertyAccess2, parameter);
    
                Expression expression = null;
                if (sortOrder == "Desc")
                {
                    expression = Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { stuType, propertyType }, items.Expression, Expression.Quote(orderExpression));
                }
                else
                {
                    try
                    {
                        expression = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { stuType, propertyAccess2.Type }, items.Expression, orderExpression);
                    }
                    catch (InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.InnerException);
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                        throw;
                    }
                }
                return items.Provider.CreateQuery<T>(expression);
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 14, 2012 7:50 AM