ai didi

c# - Google Drive API Q 逻辑子句顺序

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:23 24 4
gpt4 key购买 nike

所以我在尝试列出我的驱动器中的某些文件时遇到了问题,原始代码是:

var request = _DriveService.Files.List();
request.Q = $"name = '{Properties.Settings.Default.SomeFileName0}'" +
$" or name = '{Properties.Settings.Default.SomeFileName1}'" +
$" or name = '{Properties.Settings.Default.SomeFileName2}'" +
$" or name = '{Properties.Settings.Default.SomeFileName3}'" +
" and trashed = false" +
$" and '{Properties.Settings.Default.MyParents[0]}' in parents";
request.Fields = "files(id, name, parents)";
var files = await request.ExecuteAsync();

问题是它返回了 12 个文件,而不是 4 个文件。经过一些测试和调整后,我意识到 google drive 忽略了 子句,因此它正在检索所有已删除的文件。

现在,有了这段代码,它就像一个魅力:

var parentsTrashed = $"and '{Properties.Settings.Default.Myparents[0]}' in parents and trashed = false";
var request = _DriveService.Files.List();
request.Q = $"name = '{Properties.Settings.Default.SomeFileName0}' {parentsTrashed}" +
$" or name = '{Properties.Settings.Default.SomeFileName1}' {parentsTrashed}" +
$" or name = '{Properties.Settings.Default.SomeFileName2}' {parentsTrashed}" +
$" or name = '{Properties.Settings.Default.SomeFileName3}' {parentsTrashed}";
request.Fields = "files(id, name, parents)";
var files = await request.ExecuteAsync();

因此 andor 子句需要按以下顺序排列:(name and and and) OR (name and and and and) OR 。 ...

我在文档或互联网上找不到任何关于此的指示:这是一个错误还是有意为之?不应该在文档中记录下来,或者它是我只是没有找到它,还是它是我直到现在才知道的标准东西(我是业余爱好者)?

最佳答案

虽然我不知道 Drive 查询中运算符优先级的具体规则,但如果您使用括号指定您的意思,它至少会更容易阅读:

var request = _DriveService.Files.List();
request.Q = $"(name = '{Properties.Settings.Default.SomeFileName0}'" +
$" or name = '{Properties.Settings.Default.SomeFileName1}'" +
$" or name = '{Properties.Settings.Default.SomeFileName2}'" +
$" or name = '{Properties.Settings.Default.SomeFileName3}')" +
" and trashed = false" +
$" and '{Properties.Settings.Default.MyParents[0]}' in parents";
request.Fields = "files(id, name, parents)";
var files = await request.ExecuteAsync();

这样查询的形式是:

(name = name1 or name = name2 or name = name3 or name = name4)
and trashed = false and parentid in parents

(当然没有换行符。)

我怀疑在您当前的查询中,它实际上是:

name = name1 or name = name2 or name = name3 or
(name = name4 and trashed = false and parentid in parents)

关于c# - Google Drive API Q 逻辑子句顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832677/

24 4 0
文章推荐: c# - 请求具有无效的身份验证凭据。预期的 OAuth 2 访问 token 、登录 cookie 或其他有效的身份验证凭证
文章推荐: node.js - Nodejs 测试中 Mongodb 连接未关闭
文章推荐: node.js - 具有分层的 Node.js 配置文件
文章推荐: node.js - React-Router 无法在组件内导入组件
太空宇宙
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com