C
C#4mo ago
RacerDelux

Dynamic Order By Entity Framework

I have the following query:
context.Posts.Where(p => p.isActive).OrderBy(p => p.Name.StartsWith(searchText));
context.Posts.Where(p => p.isActive).OrderBy(p => p.Name.StartsWith(searchText));
I was asked to make it so that searchText can have multiple searches, IE: If I type in "Hello World", I should have the results ordered so that anything that starts with "Hello" or "World" appears first. But this needs to be dynamic in length. So the search text "Hello World Bob" would check for three items. In addition, I need to support partial matches. So "Hel Wor Bo" would at minimum return the same results as "Hello World Bob". How would I go about doing this? If I didn't need to worry about partial matches, I could just do p => searchText.Contains(p.Name). But I need to support partial queries.
6 Replies
Anton
Anton4mo ago
there's this framework for searching I forgot what it's called it should have this functionality
RacerDelux
RacerDelux4mo ago
Not sure. Looked into it more, I am pretty confident that EF doesn't have a way to do this in raw SQL this query would be complex as is (I only pasted a partial, the order by logic has a lot of steps in reality, was trying to adopt it to this new logic)
Anton
Anton4mo ago
elastic search I've seen this thing mentioned a lot the easiest way is a custom query It's got to be
Angius
Angius4mo ago
I wonder if EF.Functions.Like() would work here?
Anton
Anton4mo ago
nah it's too much logic for a single expression
RacerDelux
RacerDelux4mo ago
It does work, but it is VERY VERY VERY slow because it is also looking at a json column =/ I will celebrate the day we get rid of it
Want results from more Discord servers?
Add your server