Ordering filtered results I have the following method which under code review gives me a CA1502 avoid excessive complexity error (although if I suppress the message it will build fine):

private void OrderResults()
{
    switch (_orderColumn)
    {
        case 0:
        default:
            _fitleredResults = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase) ? _fitleredResults.OrderByDescending(x =&gt; x.Street) : _fitleredResults.OrderBy(x =&gt; x.Street);
            break;

        case 1:
            _fitleredResults = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase) ? _fitleredResults.OrderByDescending(x =&gt; x.Street) : _fitleredResults.OrderBy(x =&gt; x.Street);
            break;

        case 2:
            _fitleredResults = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase) ? _fitleredResults.OrderByDescending(x =&gt; x.City) : _fitleredResults.OrderBy(x =&gt; x.City);
            break;

        case 3:
            _fitleredResults = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase) ? _fitleredResults.OrderByDescending(x =&gt; x.Country) : _fitleredResults.OrderBy(x =&gt; x.Country);
            break;

        case 4:
            _fitleredResults = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase) ? _fitleredResults.OrderByDescending(x =&gt; x.RecID) : _fitleredResults.OrderBy(x =&gt; x.RecID);
            break;
    }
}


Explanation of variables:


_filteredResults is an IEnumerable&lt;AddressObject&gt;;
_orderColumn is an int passed in by jQuery DataTables and doesn't match the property indexes (which is why I was mapping it using a switch)


Address object class:

public class AddressObject
{
    public string City { get; set; }
    public string Country { get; set; }
    public string CountryRegionID { get; set; }
    public string Postcode { get; set; }
    public long RecID { get; set; }
    public string Street { get; set; }
}


Is there a way to make this code less complex?
Firstly, your case 1 case is identical to your case 0,default case (order by street). Is that intentional? It looks like a mistake to me because you've cascaded 0 and default but not 1. If it isn't a mistake, and they do all have the same behaviour you should cascade them all or let them all be picked up by the default case.

I normally see default as the last option in a switch statement, I feel like that might be a convention but don't have anything to back it up.

You're repeating this code a lot: _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase) put it in a variable (which will make the code more readable.

So far:

private void OrderResults()
{
    var isDescending = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase);
    switch (_orderColumn)
    {    
        case 2:
            _fitleredResults = isDescending ? _fitleredResults.OrderByDescending(x =&gt; x.City) : _fitleredResults.OrderBy(x =&gt; x.City);
            break;
        case 3:
            _fitleredResults = isDescending ? _fitleredResults.OrderByDescending(x =&gt; x.Country) : _fitleredResults.OrderBy(x =&gt; x.Country);
            break;
        case 4:
            _fitleredResults = isDescending ? _fitleredResults.OrderByDescending(x =&gt; x.RecID) : _fitleredResults.OrderBy(x =&gt; x.RecID);
            break;
        default:
            _fitleredResults = isDescending ? _fitleredResults.OrderByDescending(x =&gt; x.Street) : _fitleredResults.OrderBy(x =&gt; x.Street);
            break;
    }
}


As Heslsacher says, you could further improve by using named constants for the indexes.

I did write some additional stuff about using a method to get a Func but then I realised that RectId is a long so it all went out the window.

Edit

For posterity, I'll add in what I had written:

private void OrderResults()
{
    var isDescending = _orderDirection.Equals("desc", StringComparison.InvariantCultureIgnoreCase);
    var sortProperty = GetSortProperty();
    if (isDescending)
    {
        _filteredResults = _filteredResults.OrderByDescending(sortProperty);
    }
    else
    {
        _filteredResults = _filteredResults.OrderBy(sortProperty);
    }
}

private Func&lt;AddressObject, object&gt; GetSortProperty()
{
    switch(_orderColumn)
    {
        case 2:
            return x =&gt; x.City;
        case 3:
            return x =&gt; x.Country;
        case 4:
            return x =&gt; x.RectId;
        default: 
            return x =&gt; x.Street;
    }
}