It took me some time to realize that you use Union
in an attempt to order data by "match strength": first the ones that match exactly, then the ones that match with different case, etc. When I see Union
s with predicates my Pavlov-conditioned mind translates it into OR
s. I had to switch from thinking fast to slow.
So the problem is that there is no predictable sorting. No doubt, the chained Union
statements do produce a deterministic final sort order, but it's not necessarily the order of the Union
s, because each Union
also executes an implicit Distinct
. The general rule is, if you want a specific sort order, use OrderBy
methods.
Having said that, and taking...
var result = entities
.Where(e => e.Name.Contains(searchTerm))
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize).ToList();
...the desired result seems to be obtainable by:
var sortedEntities = result
.OrderByDescending(e => e.Name == searchTerm)
.ThenByDescending(e => e.Name.ToLower() == searchTerm.ToLower())
.ThenByDescending(e => e.Name.StartsWith(searchTerm, StringComparison.OrdinalIgnoreCase))
... etc.
(descending, because false
orders before
true)
However, if there are more matches than pageSize
the ordering will be too late. If pageSize = 20
and item 21 is the first exact match this item will not be on page 1. Which means: the ordering should be done before paging.
The first step would be to remove the .ToList()
from the first statement. If you remove it, the first statement is an IQueryable
expression and Entity Framework is able to combine the full statement into one SQL statement. The next step would be to move Skip/Take
to the end of the full statement and it'll also be part of the SQL.
var result = entities.Where(e => e.Name.Contains(searchTerm));
var sortedEntities = result
.OrderByDescending(e => e.Name == searchTerm)
.ThenByDescending(e => e.Name.ToLower() == searchTerm.ToLower())
.ThenByDescending(e => e.Name.StartsWith(searchTerm, StringComparison.OrdinalIgnoreCase))
... etc
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize).ToList();
But now a new problem blows in.
Since string comparison with StringComparison.OrdinalIgnoreCase
isn't supported Entity Framework will auto-switch to client-side evaluation for part of the statement. All of the filtered results will be returned from the database, but most of the the ordering and all of the paging will be done in memory.
That may not be too bad when the filter is narrow, but very bad when it's wide. So, ultimately, to do this right, you have to remove StringComparison.OrdinalIgnoreCase
and settle with a somewhat less refined match strength. Bringing us to the
End result:
var result = entities.Where(e => e.Name.Contains(searchTerm));
var sortedEntities = result
.OrderByDescending(e => e.Name == searchTerm)
.ThenByDescending(e => e.Name.StartsWith(searchTerm))
.ThenByDescending(e => e.Name.Contains($" {searchTerm} "))
.ThenByDescending(e => e.Name.EndsWith(searchTerm))
.ThenByDescending(e => e.Name.Contains(searchTerm))
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize).ToList();
Why "less refined"? Because, according your comments, the database collation isn't case sensitive, so SQL can't distinguish exact matches by case without adding COLLATE
statements. That's something we can't do with LINQ.
entities.Where(e => e.Name.Contains(searchTerm)).Skip((pageNumber - 1) * pageSize) .Take(pageSize).ToList();
Union
s to the requestIQueryable
and removedStringComparison
and It seems that it's working. Thanks a lot.Unions
toOrderByDescending/ThenByDescending
clauses to get the best matches on top.