从 OData 过滤器查询字符串构建表达式

发布于 2025-01-13 17:52:12 字数 687 浏览 4 评论 0原文

我有一个 Blazor WASM 应用程序,它构建了一个 $filter url 参数,该参数是使用以下命令生成的:

var dsContext = new DataServiceContext(new Uri("http://stackoverflow.com"));
var query = dsContext.CreateQuery<Product>("/api/products").Where(p => p.Category == "Cars");

因此,如果我使用“query.ToString())”,我就会返回一个像 ?$filter 这样的 url =Category eq 'Cars' 我使用它发送到我无法控制代码的外部 OData API。

我现在需要做与上面所做的相反的事情,并有一个可以将该 url 查询字符串传递给的函数,它将返回一个相当于 Where(p =>; ) 的 Expression 对象。 p.Category == "Cars") 以便我稍后可以在我的 Blazor 应用程序中使用它。我见过的所有示例都需要访问 EdmModel 或我没有的上下文,因为我只是调用 API 并且不知道任何这些信息。我确实有一个 products 类,那么有没有办法将该过滤器查询字符串转换为表达式?

I have a Blazor WASM app that builds a $filter url parameter which is generated using:

var dsContext = new DataServiceContext(new Uri("http://stackoverflow.com"));
var query = dsContext.CreateQuery<Product>("/api/products").Where(p => p.Category == "Cars");

So if I use "query.ToString())" I then get back a url like ?$filter=Category eq 'Cars' and I use that to send to an external OData API that I do not have control of the code.

I now need to do the opposite of what I have done above and have a function that I can pass that url querystring to and it will return an Expression object equivalent to Where(p => p.Category == "Cars") so that I can use it in later in my Blazor app. All of the examples I have seen require access to an EdmModel or a context which I do not have as I am just calling an API and do not know any of that information. I do have a products class though so is there anyway to convert that filter querystring into an expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文