因此我在尝试列出驱动器中的某些文件时遇到问题,原始代码为:
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忽略了and
子句,因此它正在检索所有已删除的文件。
现在,使用此代码,它就像一个魅力:
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();
因此and
和or
s子句必须按以下顺序排列:(name and and and) OR (name and and and) OR ...
。
我在文档中或在互联网上都找不到关于此的任何指示:这是错误还是故意的?是应该在文档中记录下来还是只是我没有找到它,还是直到现在我还是不知道的标准事物(我是业余爱好者)? < / p>
答案 0 :(得分:3)
虽然我不知道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)