0

With linq I want to use order by with specific column but I need two switches because i don't know how to use desc or asc in one

public class CustomersRepository : RepositoryBase<Customers>
        {
            public List<Customers> GetAll(CustomersProperties property, SortEnum sortEnum, int page, int limit)
            {
                var query = _context.Set<Customers>();

                switch (sortEnum)
                {
                    case SortEnum.Ascending:
                        switch (property)
                        {
                            case CustomersProperties.Name:
                                query = query.OrderBy(x => x.Name);
                                break;
                            case CustomersProperties.Surname:
                                query = query.OrderBy(x => x.Lastname);
                                break;
                            default:
                                throw new ArgumentOutOfRangeException("property");
                        }
                        break;
                    case SortEnum.Descending:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("sortEnum");
                }

                return query.Skip(page * limit)
                                .Take(limit).ToList();
            }
        }

Is it possible to do without two switch cases?

1
  • 1
    FYI, query.OrderBy(x => x.Name); does nothing: you need query = query.OrderBy(x => x.Name); Commented Jan 28, 2014 at 19:31

3 Answers 3

2

Be aware that query.OrderBy(x => x.Name); does not do anything since the sorted collection is returned from OrderBy, and you're not capturing that return.

That said, there's not a way to "dynamically" choose the direction in Linq. However, a conditional switch would be a little cleaner. Another option would be to capture the sort expression in a variable:

Expreccion<Func<Customers, string>> propExp;
switch (property)
{
    case CustomersProperties.Name:
        propExp = ((Customers)x => x.Name)            
        break;
    case CustomersProperties.Surname:
        propExp = ((Customers)x => x.Lastname);
        break;
    default:
        throw new ArgumentOutOfRangeException("property");
}

query = sortEnum == SortEnum.Ascending 
                  ? query.OrderBy(propExp);
                  : query.OrderByDescending(propExp);
Sign up to request clarification or add additional context in comments.

Comments

1

You could make your own overload, something like this:

public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, TKey>> keySelector, SortEnum sort) {
    switch (sort) {
        case SortEnum.Ascending:
            return source.OrderBy(keySelector);
        case SortEnum.Descending:
            return source.OrderByDescending(keySelector);
        default:
            throw new ArgumentOutOfRangeException("sort");
    }
}


// later on..
query = query.OrderBy(x => x.LastName, sortEnum);

2 Comments

That's not LINQ to Objects, so I don't think using IEnumerable and IOrderedEnumerable is a good choice.
@MarcinJuraszek Good point, changed to IQueryable. The concept remains clear anyway, I hope..
1

You can prepare sortProperty Expression first and then use it with either OrderBy or OrderByDescending:

public List<Customers> GetAll(CustomersProperties property, SortEnum sortEnum, int page, int limit)
{
    var query = _context.Set<Customers>();

    Expression<Func<Customers, string>> sortProperty;
    switch (property)
    {
        case CustomersProperties.Name:
            sortProperty = x => x.Name;
            break;
        case CustomersProperties.Surname:
            sortProperty = x => x.Lastname;
            break;
        default:
            throw new ArgumentOutOfRangeException("property");
    }

    switch (sortEnum)
    {
        case SortEnum.Ascending:
            query = query.OrderBy(sortProperty);
            break;
        case SortEnum.Descending:
            query = query.OrderByDescending(sortProperty);
            break;
        default:
            throw new ArgumentOutOfRangeException("sortEnum");
    }

    return query.Skip(page * limit)
                    .Take(limit).ToList();
}

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.