Graph API for email in c# 在按 receivedDateTime 排序时抛出 0DataError。

huangapple go评论112阅读模式
英文:

Graph API for email in c# is throwing 0DataError when I order by receivedDateTime

问题

当我取消注释Orderby行时,此代码会引发0DataError异常。

我尝试了不同的大小写,以及使用"desc",但在文档中找不到相关的帮助。

英文:

I have the following c# code:

    var result = _userClient
        .Users[_email]
        .Messages
        .GetAsync(conf =>
        {
            conf.QueryParameters.Filter = QueryFilter;
            conf.QueryParameters.Select = new string[] { "id", "receivedDateTime" };
            //conf.QueryParameters.Orderby = new string[] { "receivedDateTime asc" };
            conf.QueryParameters.Expand = new string[] { "attachments" };
            conf.QueryParameters.Top = 1;
        })
        .Result;

When I uncomment the Orderby line, this code throws a 0DataError exception.

I tried different capitalization, desc and could not find help for this in the documentation.

答案1

得分: 1

使用 $filter$orderby 在同一查询中必须遵循 一些规则 才能正常工作,否则 Graph API 会返回错误 InefficientFilter: The restriction or sort order is too complex for this operation

根据这些规则:

  1. $orderby 中出现的属性也必须在 $filter 中出现。
  2. $orderby 中出现的属性必须与 $filter 中的属性顺序相同。
  3. $orderby 中出现的属性必须在 $filter 中的任何未出现的属性之前。

你需要将 receivedDateTime 添加到筛选条件的开头:

string QueryFilter = "receivedDateTime ge 2022-06-26T07:00:00Z and startswith(subject, 'something') and from/emailAddress/address eq 'some@email' and hasAttachments eq true";
var result = _userClient
    .Users[_email]
    .Messages
    .GetAsync(conf =>
    {
        conf.QueryParameters.Filter = QueryFilter;
        conf.QueryParameters.Select = new string[] { "id", "receivedDateTime" };
        conf.QueryParameters.Orderby = new string[] { "receivedDateTime asc" };
        conf.QueryParameters.Expand = new string[] { "attachments" };
        conf.QueryParameters.Top = 1;
    })
    .Result;
英文:

Using $filter and $orderby in the same query must follow some rules to be working, otherwise Graph API returns the error InefficientFilter: The restriction or sort order is too complex for this operation.

Based on the rules:

  1. Properties that appear in $orderby must also appear in $filter.
  2. Properties that appear in $orderby are in the same order as in $filter.
  3. Properties that are present in $orderby appear in $filter before any properties that aren't.

You need to add receivedDateTime at the beginning of the filter:

string QueryFilter = "receivedDateTime ge 2022-06-26T07:00:00Z and startswith(subject, 'something') and from/emailAddress/address eq 'some@email' and hasAttachments eq true"
var result = _userClient
    .Users[_email]
    .Messages
    .GetAsync(conf =>
    {
        conf.QueryParameters.Filter = QueryFilter;
        conf.QueryParameters.Select = new string[] { "id", "receivedDateTime" };
        conf.QueryParameters.Orderby = new string[] { "receivedDateTime asc" };
        conf.QueryParameters.Expand = new string[] { "attachments" };
        conf.QueryParameters.Top = 1;
    })
    .Result;

huangapple
  • 本文由 发表于 2023年6月26日 17:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定