使用OutlookServicesClient编写高效的查询 [英] Write efficient queries with the OutlookServicesClient

查看:59
本文介绍了使用OutlookServicesClient编写高效的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Office 365邮件API 和我的目标是获取用户发送(1.)和接收(2.) 今天 .

I am using the Office 365 Mail API and my goal is to get the total number of email messages a user sent (1.) and received (2.) today.

这个,我首先在 Office 365 API Playground 中创建并尝试运行一些查询:

Todo this, I first created and tried and run some queries in the Office 365 API Playground:

  1. https://outlook.office.com/api/v2.0/me/mailfolders/sentitems/messages?$filter=sentdatetime%20ge%202015-12-10T08:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients
  2. https://outlook.office.com/api/v2.0/me/messages?$count=true&$filter=receiveddatetime%20ge%202015-12-09T10:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients
  1. https://outlook.office.com/api/v2.0/me/mailfolders/sentitems/messages?$filter=sentdatetime%20ge%202015-12-10T08:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients
  2. https://outlook.office.com/api/v2.0/me/messages?$count=true&$filter=receiveddatetime%20ge%202015-12-09T10:00:00.000Z&$select=Subject,CreatedDateTime,ToRecipients

现在,我正在努力使用OutlookServicesClient API编写这些查询.我没有发现很多示例,这些示例超出了非常简单的查询范围……到目前为止,我所得到的是:

Now, I am struggling with writing these queries with the OutlookServicesClient API. I didn't find many examples, which go beyond very simple queries... What I have so far:

  1. var mailResults = await client.Me.MailFolders.Where(f => f.DisplayName == "Sent Items").ExecuteAsync();尚未返回仅邮件并过滤当前日期.

var mailResults = await client.Me.Messages.Where(m => m.ReceivedDateTime.Value == date.UtcDateTime).ExecuteAsync();即使我收到很多电子邮件,不返回任何结果.此外,我想排除在群集",已删除邮件"和垃圾邮件"文件夹中收到的电子邮件.

var mailResults = await client.Me.Messages.Where(m => m.ReceivedDateTime.Value == date.UtcDateTime).ExecuteAsync(); Does not return any results, even though I received many emails. Further, I'd like to exclude emails which I received in folders 'Cluster', 'Deleted Items' and 'Junk Email'.

通常,我不确定用英语文件夹名称进行过滤是否是一个好主意,因为我需要更改其他语言的代码.特殊的Outlook文件夹是否有特殊的ID,例如已发送邮件",垃圾邮件",群集"等?

Generally, I am not sure if it's a good idea to filter with English folder names, as I would need to change the code for other languages. Are there special Ids for the special Outlook folders, such as Sent Items, Junk Email, Cluster, etc.?

另外,要解决我的两个请求,我只能获取所有电子邮件并自己处理过滤,但这并不高效,并且该API已经支持过滤(如在原始请求中所示),我不确定如何使用 OutlookServicesClient API 编写它们.

Additionally, to solve my two requests, I could just fetch all emails and handle the filtering by myself, but that's not efficient and the API already supports filtering (as can be seen in the raw requests), I am just not sure how to write them with the OutlookServicesClient API.

推荐答案

通常OutlookServicesClient使用LINQ来构建其查询,因此您需要使用Where方法来构建$filter查询参数.例如,如果您希望今天收到所有消息,则可以执行以下操作:

Generally the OutlookServicesClient uses LINQ to build it's queries, so you'd need to use the Where method to build a $filter query parameter. If you wanted to get all messages received today, for example, you would do something like:

DateTimeOffset startOfDay = DateTimeOffset.Now.Date.ToUniversalTime();

client.Me.Messages.Where(m => m.ReceivedDateTime >= startOfDay).ExecuteAsync();

关于您的问题:

  1. 不按文件夹名称过滤. API具有常量文件夹 收件箱,已删除邮件,已发送邮件和草稿的ID.所以要 您将要执行的已发送邮件"文件夹:

  1. Don't filter by the name of the folder. The API has constant folder IDs for Inbox, Deleted Items, Sent Items, and Drafts. So to get the Sent Items folder you would do:

client.Me.MailFolders.GetById("SentItems")

  • 您的查询Where(m => m.ReceivedDateTime.Value == date.UtcDateTime)不会返回值,因为您正在测试datetime值与常量是否相等,几乎永远不会返回结果.比较会下降到秒级,因此,除非您在date变量中的日期和时间确切地收到了邮件,否则您将找不到匹配项.
  • Your query Where(m => m.ReceivedDateTime.Value == date.UtcDateTime) wouldn't return values because you're testing that the datetime value is equal to a constant, which is pretty much never going to return a result. The comparison goes down to the seconds level, so unless you have messages received exactly at the date and time in your date variable, you'll get no matches.
  • 我写了一些我认为符合您意图的查询:

    I wrote up some queries that I think match your intent:

    DateTimeOffset startOfDay = DateTimeOffset.Now.Date.ToUniversalTime();
    
    var receivedMessages = await client.Me.Messages
      // $orderby=ReceivedDateTime desc
      .OrderByDescending(m => m.ReceivedDateTime)
      // $filter=ReceivedDateTime ge 2015-12-11T05:00:00Z
      .Where(m => m.ReceivedDateTime >= startOfDay)
      // $top=10
      .Take(10)
      // $select=Subject,ReceivedDateTime,From
      .Select(m => new { m.Subject, m.ReceivedDateTime, m.From })
      .ExecuteAsync();
    
    string resultMessage = "";
    foreach (var message in receivedMessages.CurrentPage)
    {
      resultMessage += "Received: " + message.ReceivedDateTime.ToString() + " from " + message.From.EmailAddress.Address
                       + ": " + message.Subject + "\n";
    }
    
    MessageBox.Show(resultMessage, "Received messages");
    
    var sentMessages = await client.Me.MailFolders.GetById("SentItems").Messages
      // $orderby=SentDateTime desc
      .OrderByDescending(m => m.SentDateTime)
      // $filter=SentDateTime ge 2015-12-11T05:00:00Z
      .Where(m => m.SentDateTime >= startOfDay)
      // $top=10
      .Take(10)
      // $select=Subject,ReceivedDateTime,From
      .Select(m => new { m.Subject, m.SentDateTime, m.ToRecipients })
      .ExecuteAsync();
    
    resultMessage = "";
    foreach (var message in sentMessages.CurrentPage)
    {
      resultMessage += "Sent: " + message.SentDateTime.ToString() + " to " + message.ToRecipients.Count
                     + " recipients: " + message.Subject + "\n";
    }
    
    MessageBox.Show(resultMessage, "Sent messages");
    

    这篇关于使用OutlookServicesClient编写高效的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆