C# Predicatebuilder gives error on double word

59 views Asked by At

I have an algorithm for searcing text under a pagination endpoint like "http://localhost:8001/api/endusers?count=20&offset=0&search=mert ozan,FirstName,MiddleName,LastName,Email,CompanyName"

So, My code takes "mert ozan" as a searchText, and other fields are our search target. This searchText gives correct result with only one sentence like "mert" finds mert in any cases. But if searchText is "mert ozan" it does not work. What should I change for this situation?

Here is my code block:

if (!string.IsNullOrEmpty(search))
{
    var searchParams = search.Split(',', StringSplitOptions.TrimEntries);
    if (searchParams.Length > 1)
    {
        var searchText = searchParams.First();
        var searchFields = searchParams.Where((_, index) => index != 0).ToList();

        var predicate = PredicateBuilder.New<AdminEnduserPaginationView>();
        searchFields.ForEach((x) =>
        {
            predicate = predicate.Or(p => EF.Functions.Like(EF.Property<string>(p, x), searchText + "%"));
        });

        endUserQuery = endUserQuery.Where(predicate);
    }
}

What I Tried:

I attempted to search using a multi-field query with "mert ozan" as the searchText.

Expected Outcome:

I expected the code to correctly retrieve results for "mert ozan" in any of the specified search fields. ** Actual Result:**

The code worked for single words (e.g., "mert") but failed for the full "mert ozan." It doesn't handle multi-word searches as expected. Looking for guidance to make it work correctly.

0

There are 0 answers