6

I'm checking the sort parameter and building a bunch of if statements:

if (sortDirection == "ASC")
{
    if (sortBy == "Id")
        return customerList.OrderBy(x => x.Id).Skip(startIndex).Take(pageSize).ToList();
    if (sortBy == "FirstName")
        return customerList.OrderBy(x => x.FirstName).Skip(startIndex).Take(pageSize).ToList();
    if (sortBy == "City")
        return customerList.OrderBy(x => x.City).Skip(startIndex).Take(pageSize).ToList();
}
else
{
    if (sortBy == "Id")
        return customerList.OrderByDescending(x => x.Id).Skip(startIndex).Take(pageSize).ToList();
    if (sortBy == "FirstName")
        return customerList.OrderByDescending(x => x.FirstName).Skip(startIndex).Take(pageSize).ToList();
    if (sortBy == "City")
        return customerList.OrderByDescending(x => x.City).Skip(startIndex).Take(pageSize).ToList();
}

How do I make this better?

7
  • 6
    Define "better". Better for what? Commented May 11, 2012 at 19:49
  • 1
    In what way do you want to "improve" it? Does it not work as intended? Is it too slow? Do you not like how the code is structured? We need more information here. Commented May 11, 2012 at 19:49
  • I would recomend using LINQ composition. See stackoverflow.com/questions/5881107/… Commented May 11, 2012 at 19:50
  • 1
    Create a dictionary of delegates and then call them basing on the key(s). Commented May 11, 2012 at 19:51
  • It'd be nice if it can be more dynamic where I don't need to add if statements just because a new sortBy string has been sent in. Commented May 11, 2012 at 19:51

3 Answers 3

8

Separate your ordering and the rest of the query - the parts that are the same for each query you don't have to duplicate in your codebase (keep it DRY):

var query = customerList;

if (sortDirection == "ASC")
{
    if (sortBy == "Id")
       query = query.OrderBy(x => x.Id);
    ///and so on
}

query = query.Skip(startIndex).Take(pageSize).ToList();
Sign up to request clarification or add additional context in comments.

11 Comments

That's still a lot of boilerplate for all the fields, wouldn't reflection be much cleaner? Imagine if he had 50 fields! :)
@NiklasB. That would be problem, because OrderBy is generic and the lambda has different type for every property. Unless you want to convert it to object everywhere.
@mattytommo: That's certainly possible and neat if this is an in-memory collection
@Euphoric: Oh, that certainly would be a problem. I don't know C# too well :)
@Euphoric - You could create a Dictionary<string,IComparable>.
|
2

Use reflection :)

customerList = (sortDirection == "ASC")
   ? customerList
        .OrderBy(x => x.GetType().GetProperty(sortBy).GetValue(x, null))
        .Skip(startIndex)
        .Take(pageSize)
        .ToList()
   : customerList
        .OrderByDescending(x => x.GetType().GetProperty(sortBy).GetValue(x, null))
        .Skip(startIndex)
        .Take(pageSize)
        .ToList();

Comments

1

It looks like you simply want to order by the property name as a string. In which case, this is actually already solved by using "Dynamic LINQ":

Dynamic LINQ OrderBy on IEnumerable<T>

Take a look at this question's answer and it should provide you with sample code to solve your problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.