OData 查询中的 LIKE
我正在将项目从使用 linq2sql 转换为使用 odata 源。我正在尝试查找这样的用户...
FrameworkEntities db = new FrameworkEntities(
new Uri("http://localhost/odata/FrameworkService.svc"));
User user = db.Users.Where(
u => SqlMethods.Like(u.UserName, UserName)).FirstOrDefault();
但收到错误“方法‘Boolean Like(System.String, System.String)’不能在客户端上使用;它仅用于转换为 SQL。”这在 linq2sql 中是可能的,所以我只是想知道如何更改它,以便它可以与 odata 服务一起使用。我还没有找到任何有关它的文章。
I am converting a project from using linq2sql to use an odata source. I am trying to lookup a user like this...
FrameworkEntities db = new FrameworkEntities(
new Uri("http://localhost/odata/FrameworkService.svc"));
User user = db.Users.Where(
u => SqlMethods.Like(u.UserName, UserName)).FirstOrDefault();
but am getting the error "Method 'Boolean Like(System.String, System.String)' cannot be used on the client; it is only for translation to SQL." This was possible in linq2sql so I am just wondering how I would change this so that it would work with the odata service. I haven't found any articles about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如错误所示,SqlMethods.Like() 用于转换为 SQL。 o数据查询不会转换为 SQL,因此您无法使用该方法。最好的选择是将 IndexOf 与 StringComparison.InvariantCultureIgnoreCase 结合使用来模拟不区分大小写的 String.Contains 版本。
哦...您可以将调用组合到:Where
和FirstOrDefault
Like the error says, SqlMethods.Like() is for translation to SQL. oData queries don't get translated to SQL, so you can't use the method. Your best bet is to use IndexOf with
StringComparison.InvariantCultureIgnoreCase
to emulate a case insensitive version ofString.Contains
.Oh...and you can combine your calls to:Where
andFirstOrDefault
OData 确实包含 substringof 函数。对于我最近从事的一个小项目,我担心数据模型的三个属性中是否存在“类似于”字符串的数据。
我还在模型属性周围使用了 toupper 函数,并结合大写表单输入搜索字符串,以便进行不区分大小写的搜索。所以在这里,我担心 IN_OUT 在三个字段(BENEFICIARY、ORIGINATOR 或 MEMO)中的一个或多个字段中为“0”和“D20033”。
OData does contain a substringof function. For a small project I recently worked on, I was concerned if there was data 'LIKE' a string in three of the properties of the data model.
I also used the toupper function around the model properties in conjunction with capitalizing the form-input search string so that I had a case-insensitive search. So here, I was worried about IN_OUT being '0' and 'D20033' in one or more of the three fields, BENEFICIARY, ORIGINATOR or MEMO.
或者,您可以在 Odata 中使用“startswith”过滤表达式。
示例:
这将生成如下 URI:
http://localhost:7048/OData/Users?$filter=UserName 以“奥巴马”开头
参考:
https://msdn.microsoft.com/en-我们/library/hh169248(v=nav.80).aspx
Alternatively you can use 'startswith' Filter Expressions in Odata.
Example:
This will generate a URI like this:
http://localhost:7048/OData/Users?$filter=UserName startswith 'Obama'
Reference:
https://msdn.microsoft.com/en-us/library/hh169248(v=nav.80).aspx