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

回眸只為那壹抹淺笑 提交于 2020-01-02 22:32:21

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/11615864/odatawildcard-startswith-filtering-for-number-id-fields-in-the-url-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!