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;
}
Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
andrea
  • 381
  • 5
  • 23
  • check this answer http://stackoverflow.com/questions/26699105/library-for-jquery-datatables-asp-net-mvc – py3r3str Nov 04 '14 at 16:16
  • moving with these solution, thanks for the comment, i'm arriving to a similar solution http://stackoverflow.com/questions/26759416/generics-c-sharp-organization-of-methods-that-depends-on-type – andrea Nov 05 '14 at 14:18

1 Answers1

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");
Community
  • 1
  • 1
Agat
  • 4,577
  • 2
  • 34
  • 62