OData:通配符(开始)过滤url请求中的数字(ID)字段 [英] OData:Wildcard (startswith) filtering for number (ID) fields in the url request

查看:201
本文介绍了OData:通配符(开始)过滤url请求中的数字(ID)字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一种通过OData和JavaScript对我的实体的数字ID字段进行模糊搜索的方法。到目前为止,我还没有找到我要找的答案。我可以使用Startswith过滤器选项完美地过滤其他edm.string列,但是当我尝试传入任何其他非字符串类型时,我从服务器返回一个类型错误响应。

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.

在我控制数据库的应用程序中,通过创建我需要的视图并将视图的数字类型转换为字符串,我成功地解决了这个问题。但是,这样做似乎有点过头了,为了整个数据集创建一个视图,所以我可以允许用户通配符搜索ID,否则OData查询工作得非常好。

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!

推荐答案

不幸的是,我认为你已经发现了一个最好的解决方案(创建一个视图)。您还可以创建一个允许您进行模糊搜索的服务操作。

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.

您的后端使用了什么? LINQ-to-Entities不支持此功能,但您可以创建类似于此的服务操作(为了证明它可以工作,您可以填充 ToList()产品之后在那里打电话,确保不要将这样的东西部署到生产中:)):

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));
    }
    // ...
}

不是问我们已经听过很多,但我绝对可以在团队中提出来,当我们开始OASIS标准化过程时,我们可以考虑这个问题。

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.

这篇关于OData:通配符(开始)过滤url请求中的数字(ID)字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆