muck41
muck41

Reputation: 288

OData:Wildcard (startswith) filtering for number (ID) fields in the url request

I've been researching a way to perform fuzzy searches for my numerical ID fields of my entity via OData and JavaScript. So far, I have not found the answer I am looking for. I can filter other edm.string columns perfectly using the "Startswith" filter option, however when i attempt to pass in any other non-string type, I get a type error response from the server back.

In applications that I control the database, I was successfully able to get around this, by creating views I need and converting the numerical type of the view to a string. However, this seems a bit overkill to go out of my way and create a view for an entire set of data just so I can allow a user to wildcard search the ID, when otherwise the OData query works perfectly well.

Has anyone found a good solution to this? Thank you!

Upvotes: 1

Views: 3156

Answers (1)

Mark Stafford - MSFT
Mark Stafford - MSFT

Reputation: 4336

Unfortunately I think you've already discovered one of the best solutions (creating a view). You could also create a service operation that allows you to do fuzzy search.

What are you using for your backend? This isn't supported on LINQ-to-Entities, but you might be able to create a service operation that looks something like this (to prove that it can work, you can stuff a ToList() call in there after Products, just be sure not to deploy something like that to production :)):

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ScratchService : DataService<ScratchContext>
{

    [WebGet]
    public IQueryable<Product> FuzzySearch(string idStartsWith)
    {
        var context = new ScratchContext();
        return context.Products.Where(p => p.ID.ToString().StartsWith(idStartsWith));
    }
    // ...
}

It's not an ask we've heard a lot, but I can definitely bring it up on the team and as we start the OASIS standardization process it's something we can be thinking about.

Upvotes: 2

Related Questions