C#Google Calendar API v3:所有日历的自由文本搜索

时间:2014-08-28 19:05:05

标签: c# google-calendar-api

我正在构建一个Windows窗体应用程序,并利用DataGridView显示我的google CalendarEventsList的自由文本(q)搜索结果。但是,我的搜索速度慢得令人无法接受,并且总是会返回类似于谷歌日历应用程序中内置搜索的结果。

我的日历列表中有33个日历。当我的表单加载时,我将CalendarId和Summary加载到公共DataTable中。

修改

经过仔细调查,我已经意识到随意使用数据表的垮台:它们变得缓慢而臃肿。下面的代码已被修改为使用列表类型和LINQ。这很有效,我的问题也解决了。

    public List<CalendarListEntry> ltCalendarList;
    private void GetUserCalendarList()
    {
        MyCalenderControl cal = new MyCalenderControl();

        List<CalendarListEntry> calList = cal.GetCalLists(cal.GoogleAuthorize())
            .Select(s => new CalendarListEntry()
            {
                Id = s.Id,
                Summary = s.Summary
            })
            .OrderBy(x => x.Summary).ToList();

        ltCalendarList = calList;

        InvokeCalendar_cbx(calList);
    }

用户定义用于设置q的字符串。此时,我循环遍历calendar:list datatable中的每一行,并使用Id列在每个日历中搜索包含最小和最大日期之间q的任何事件。

    private void CalendarSearch(object search)
    {
        if (ltCalendarList != null && ltCalendarList.Count() > 0)
        {
            string min = dtpStart.Value.ToString("yyyy-MM-dd'T'hh:mm:sszzz");
            string max = dtpEnd.Value.ToString("yyyy-MM-dd'T'hh:mm:sszzz");
            string[] strSearch = search.ToString().Split(' ');
            string strSearchSplit = strSearch[0] + " " + strSearch[1];

            MyCalenderControl cal = new MyCalenderControl();
            List<CalendarListEntry> lt = ltCalendarList;
            List<Event> ltResult = new List<Event>();

            //Authorize calendar access outside of main loop.
            CalendarService service = cal.GoogleAuthorize();

            //loop through each calenderid.
            for (int i = 0; i < lt.Count; i++)
            {
                //linq to Id in calenderlist
                var calId = lt.Select(n => n.Id ).ElementAt(i).ToString();

                //query n calenderid for q (strSearchSplit)
                Events events = cal.ListEvents(service, calId, min, max, strSearchSplit);

                //if query returns items, utilize them
                if (events.Items.Count() > 0)
                {
                    //linq an unused list property of type string, and set to the name of the calender for display
                    var query = events.Items.Select(x => { x.Kind = events.Summary; return x; });

                    //Add query results to the list
                    ltResult.AddRange(query);

                    //linq filter resutlts, and add them to the datagridview
                    var filter = ltResult
                        .Select(s => new { Calender = s.Kind, Scheduled = s.Start.DateTime, Title = s.Summary
                            , Notes = s.Description, s.Creator.DisplayName, s.Created })
                        .OrderByDescending(o => o.Scheduled)
                        .ToList();

                    InvokeCalendar_DGV(filter);
                }
            }
            if (ltResult.Count <= 0)
            {
                dgvCalendarSchemaInitiate("Google Calendar API returned no results.");
            }
        }
    }

使用API​​ v3,我似乎需要在每个日历中搜索q事件。

    protected internal List<Event> ListEvents(CalendarService service, string id, string min, string max, string strQ = "")
    {
        EventsResource.ListRequest calRequest = service.Events.List(id);
        calRequest.TimeMin = Convert.ToDateTime(min);
        calRequest.TimeMax = Convert.ToDateTime(max);
        //calRequest.Fields = "items(start,summary),summary";
        calRequest.Q = strQ;

        Events response = calRequest.Execute();
        List<Event> items = (List<Event>)response.Items;

        return items;
    }

最终,我已经编程了6个月,我选择每天刻意学习。

0 个答案:

没有答案