1

i'm using Datatables Jquery Extension and i've made multicolum sorting reading those posts:

My question is, those two methods are dependent to The model that i'm using in my Controller, in this case

If i need to use similar code in other controllers i need to replicate that and changing model fields and the Type with others, for example .

This seems to me not very DRY.

How to Proceed? Is controller the good position for those two methods?

private IOrderedQueryable<User> CreateSortedQuery(DataTableParameterModel parameterModel, IQueryable<User> baseQuery)
{
    var orderedQuery = (IOrderedQueryable<User>)baseQuery;

    for (int i = 0; i < parameterModel.iSortingCols; ++i)
    {
        var ascending = string.Equals("asc", parameterModel.sSortDir[i], StringComparison.OrdinalIgnoreCase);
        int sortCol = parameterModel.iSortCol[i];

        Expression<Func<User, string>> orderByExpression = GetOrderingFunc(sortCol);
        if (orderByExpression != null)
        {
            if (ascending)
            {
                orderedQuery = (i == 0)
                    ? orderedQuery.OrderBy(orderByExpression)
                    : orderedQuery.ThenBy(orderByExpression);
            }
            else
            {
                orderedQuery = (i == 0)
                    ? orderedQuery.OrderByDescending(orderByExpression)
                    : orderedQuery.ThenByDescending(orderByExpression);
            }
        }
        else
        {
            if (ascending)
            {
                orderedQuery = (i == 0)
                    ? orderedQuery.OrderBy(c => c.Id)
                    : orderedQuery.ThenBy(c => c.Id);
            }
            else
            {
                orderedQuery = (i == 0)
                    ? orderedQuery.OrderByDescending(c => c.Id)
                    : orderedQuery.ThenByDescending(orderByExpression);
            }
        }

    }
    return orderedQuery;
}


private Expression<Func<User, string>> GetOrderingFunc(int ColumnIndex)
{
    Expression<Func<User, string>> InitialorderingFunction;
    switch (ColumnIndex)
    {
        case 1:
            InitialorderingFunction = c => c.FirstName;
            break;
        case 2:
            InitialorderingFunction = c => c.LastName;
            break;
        case 3:
            InitialorderingFunction = c => c.UserName;
            break;
        case 4:
            InitialorderingFunction = c => c.Email;
            break;
        case 5:
            InitialorderingFunction = c => c.BusinessName;
            break;
        default:
            InitialorderingFunction = null;
            break;
    }

    return InitialorderingFunction;
}
2

1 Answer 1

0

I guess, your question is pretty close to these two answers:

Property name evaluating from expression:

public static RouteValueDictionary GetInfo<T,P>(this HtmlHelper html, Expression<Func<T, P>> action) where T : class
{
    var expression = (MemberExpression)action.Body;
    string fieldName = expression.Member.Name;

and

Applying linq sorting passing string values with LINQ Dynamic Query Library:

var result = data
    .Where(/* ... */)
    .Select(/* ... */)
    .OrderBy(fieldName + " asc");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.