Is there any way I can refactor this expression tree?    public static IOrderedQueryable<T_PO> SortedPO(this IQueryable<T_PO> query,  ExtjsGridPagingParameter.SortingParameter sortingParameter)    {        IOrderedQueryable<T_PO> result = null;        if (String.IsNullOrEmpty(sortingParameter.property) == false)        {                            Expression<Func<T_PO, string>> orderField = null;            Expression<Func<T_PO, DateTime?>> orderFieldDate = null;            Expression<Func<T_PO, decimal>> orderFieldDecimal = null;            switch (sortingParameter.property)            {                case po_code:                    orderField = (item => item.po_code);                    break;                case proj_name:                    orderField = (item => item.T_PROJECT.proj_name);                    break;                case po_supplier_contact:                    orderField = (item => item.po_supplier_contact);                    break;                case po_grandtotal:                    orderFieldDecimal = (item => item.po_grandtotal);                    break;                case po_docdate:                    orderFieldDate = (item => item.po_docdate);                    break;                default:                    break;            }            if (orderField != null)            {                if (sortingParameter.direction == ASC)                {                    result = query.OrderBy(orderField);                }                else                {                    result = query.OrderByDescending(orderField);                }            }            else if (orderFieldDate != null)            {                if (sortingParameter.direction == ASC)                {                    result = query.OrderBy(orderFieldDate);                }                else                {                    result = query.OrderByDescending(orderFieldDate);                }            }            else if (orderFieldDecimal != null)            {                if (sortingParameter.direction == ASC)                {                    result = query.OrderBy(orderFieldDecimal);                }                else                {                    result = query.OrderByDescending(orderFieldDecimal);                }            }        }        return result;    }
Refactor Linq Expression
c#;linq;expression trees
Note that your code will return null in case if the sort order is not defined. I've preserved this logic, but you may want to change it to return original query (and thus change return type to IQueryable<T_PO>).You are doing 2 separate actions in this method, so if you split it into 2 parts you can get more compact and readable code:public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool isAscending){    return isAscending        ? source.OrderBy(keySelector)        : source.OrderByDescending(keySelector);}public static IOrderedQueryable<T_PO> SortedPO(this IQueryable<T_PO> query,  ExtjsGridPagingParameter.SortingParameter sortingParameter){    bool isAscending = sortingParameter.direction == ASC;    switch (sortingParameter.property)    {        case po_code:            return query.OrderBy(item => item.po_code, isAscending);        case proj_name:            return query.OrderBy(item => item.T_PROJECT.proj_name, isAscending);        case po_supplier_contact:            return query.OrderBy(item => item.po_supplier_contact, isAscending);        case po_grandtotal:            return query.OrderBy(item => item.po_grandtotal, isAscending);        case po_docdate:            return query.OrderBy(item => item.po_docdate, isAscending);        default:            return null;    }}