Our application has a search screen in which users search a very large database with doctor and nurse records. They can specify:

  1. Search String
  2. Filters
  3. Sort Order

Any combination is possible: sometimes only a search string is given and in other cases a search string, filters, and multiple sort orders are given.

The query is created in LINQ. Everything is fine, but translating the Sort Orders into LINQ are causing us a huge headache. For example, one user may need to sort by FirstName ASC, LastName ASC, and City ASC, whereas another may need State DESC, Gender ASC, Category ASC. etc.etc.

To build an if statement with all possible combinations is a nightmare (read almost impossible) as it creates an explosion of possible OrderBy().ThenByDescending() combinations.

How is this handled in LINQ? There does not seem to be an obvious solution.