OData 查询中的 LIKE

发布于 2024-09-29 17:33:00 字数 436 浏览 4 评论 0原文

我正在将项目从使用 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥枳 2024-10-06 17:33:00

正如错误所示,SqlMethods.Like() 用于转换为 SQL。 o数据查询不会转换为 SQL,因此您无法使用该方法。最好的选择是将 IndexOf 与 StringComparison.InvariantCultureIgnoreCase 结合使用来模拟不区分大小写的 String.Contains 版本。

哦...您可以将调用组合到 WhereFirstOrDefault

User user = db.Users
              .Where(u => u.UserName.IndexOf(Username, 
                  StringComparison.InvariantCultureIgnoreCase) > 0)
              .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 of String.Contains.

Oh...and you can combine your calls to Where and FirstOrDefault:

User user = db.Users
              .Where(u => u.UserName.IndexOf(Username, 
                  StringComparison.InvariantCultureIgnoreCase) > 0)
              .FirstOrDefault();
望笑 2024-10-06 17:33:00

OData 确实包含 substringof 函数。对于我最近从事的一个小项目,我担心数据模型的三个属性中是否存在“类似于”字符串的数据。

$filter=(IN_OUT eq '0' and (substringof('D20033', toupper(BENEFICIARY)) or substringof('D20033', toupper(ORIGINATOR)) or substringof('D20033', toupper(MEMO))))

我还在模型属性周围使用了 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.

$filter=(IN_OUT eq '0' and (substringof('D20033', toupper(BENEFICIARY)) or substringof('D20033', toupper(ORIGINATOR)) or substringof('D20033', toupper(MEMO))))

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.

血之狂魔 2024-10-06 17:33:00

或者,您可以在 Odata 中使用“startswith”过滤表达式。

示例:

    var query = context.Users;
    query = query.AddQueryOption("$filter", String.Format("UserName startswith {0}", UserName));
    Users sers = new DataServiceCollection<User>(query);

这将生成如下 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:

    var query = context.Users;
    query = query.AddQueryOption("$filter", String.Format("UserName startswith {0}", UserName));
    Users sers = new DataServiceCollection<User>(query);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文