簡體   English   中英

帶有字典的Google Calendar API v3循環僅保存1

[英]Google Calendar API v3 loop with Dictionary saves just 1

嘿,我在下面使用以下代碼將每個日歷事件保存到字典中。

Dictionary<int, GoogleCalEvents> GEvents;

public class GoogleCalEvents
{
    public string timeFrom { get; set; }
    public string timeTo { get; set; }
    public TimeSpan duration { get; set; }
    public string summary { get; set; }
    public string description { get; set; }
    public string status { get; set; }
    public string organizer { get; set; }
    public EventAttendee attendees { get; set; }
    public int ID { get; set; }
}

public Dictionary<int, GoogleCalEvents> getCalEvents()
{
   foreach (var eventItem in events.Items)
   {
      GEvents = new Dictionary<int, GoogleCalEvents>()
      {
           {
                loopID,
                new GoogleCalEvents {
                     timeFrom    = from,
                     timeTo      = to,
                     duration    = duration,
                     summary     = summary,
                     description = description,
                     status      = status,
                     organizer   = organizer,
                     attendees   = attendees,
                     ID          = loopID
                 }
            }
       };

       loopID++;
    }

    return GEvents;
}

並調用該類:

calendarAPI myClass = new calendarAPI();
Dictionary<int, GoogleCalEvents> blah = myClass.getCalEvents();

在執行foreach時,我可以看到它在第一個復飛中放置了0 ,然后在第二個復飛中放置了1 但是,在循環過程中,每次復飛GEvent的只有 1 返回GEvents后,它在字典中只有最后插入的項目嗎?

如何設置不正確?

您正在循環中重新創建字典。 你應該只做一次

public Dictionary<int, GoogleCalEvents> getCalEvents()
{
    var loopID=0;
    var GEvents = new Dictionary<int, GoogleCalEvents>();
    foreach (var eventItem in events.Items)
    {

        GEvents.Add(loopID, new GoogleCalEvents
        {
            timeFrom = from,
            timeTo = to,
            duration = duration,
            summary = summary,
            description = description,
            status = status,
            organizer = organizer,
            attendees = attendees,
            ID = loopID
        });
    }
    loopID++;

    return GEvents;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM