2013-07-22 60 views
0

你知道我該怎麼做嗎?如何將int數組轉換爲字典?

public override IDictionary<int, DateTime> GetList() 
    { 
     var AdList = new Dictionary<int, DateTime>(); 
     AdList = client.GetAdList(loginTicket); //it returns int array. 
     // I've also tried AdList =client.GetAdList(loginTicket).ToDictionary<int, DateTime>(); it doesn't work.. 
     return AdList; 
    } 
+0

生成日期? – Sergio

+0

此函數返回在給定日期(DateTime持有它)之前發佈的廣告列表.Sergio,感謝您的回覆。 – Gece

回答

0

好了,不用你打算如何獲得日期,或者你從哪裏得到電磁信息,但我可以建議一些代碼:過得好去

public static IDictionary<int, DateTime> GetList() 
{ 
    var AdList = new Dictionary<int, DateTime>(); 
    var intArr = GetAdList(); //get your int[] array or List<int> 

    //if method GetAdList() returns List<int> use this line 
    intArr.ForEach(a => AdList.Add(a, DateTime.Now)); 

    //otherwise if method returns int[] array use this line 
    Array.ForEach(intArr, a => AdList.Add(a, DateTime.Now)); //populate dictionary with array elements as keys and DateTime.Now as values 

    return AdList; 
} 
+0

Imm,我得到了「方法的類型參數」System.Array.ForEach (T [],System.Action )'不能從用法推斷,請嘗試明確指定類型參數。錯誤.. – Gece

+0

@Gece更新了我的答案。你的方法返回'List ',而你說它返回'int array',所以你必須使用'List'類方法 – Sergio

+0

GetList返回一個包含ID(鍵)和LastUpdateDate(值)的字典Sergio和GetAdList返回一個int陣列。我不知道我該怎麼處理它。我試過'ICollection key = AdArr.Value;'但是當我試着AdList.Keys = key;我得到一個關於「屬性或索引器」System.Collections.Generic.Dictionary .Keys'不能被分配給 - 它是隻讀的「的錯誤。 – Gece