asp.netsortingsearchrazor-pages

How To Sort A searchString on a Razor Index Page


I have a Razor Page Index that the user would like to have the search results sorted a specific way.

Currently, the searchString can search for several different fields. How would I search for each, but have a different sortOrder on each one?

Here is the code:

        if (!string.IsNullOrEmpty(searchString))
            trip = trip.Where(s => s.ClientName.Contains(searchString) || s.CaseWorkerName.Contains(searchString) || s.PickUpDriver.Contains(searchString) || s.ReturnDriver.Contains(searchString) || s.PickUpDateTime.Value.ToString().Contains(searchString)).OrderBy(s => s.PickUpDateTime);

The .OrderBy(s => s.PickUpDateTime) was my feeble attempt to at least get all of them to sort that way after the search results were returned. Although it doesn't give me errors, it doesn't sort by the PickUpDateTime either!

Ideally, I would like to be able to sort each type of search (ie, Client Name, CaseWorkerName, PickUpDriver, ReturnDriver, and PickUpDateTime independently (in other words, ClientName might be sorted by LastName then FirstName, PickUpDateTime would be sorted by itself in ascending order, etc) Could someone please tell me what I am doing wrong? Thanks so much.


Solution

  • User changed mind. Ended up being able to accomplish what she wanted in the case statement for the sorting.