![](/img/trans.png)
[英]How do I Copy the Values of an IDictionary into an IList Object in .Net 2.0?
[英]How do I bind a IList of IDictionary objects to the collection of a data source?
我有以下聲明:
IList<IDictionary<DateTime, string>> actions = new List<IDictionary<DateTime, string>>();
IDictionary<DateTime, string> d;
然后我多次添加動作,例如:
d = new Dictionary<DateTime, string>();
d.Add(DateTime.Now, "Must select at least one Policy to assign.");
actions.Add(d);
最后,我希望能夠將此List of Dictionary對象分配給數據源。 就像是:
rptActions.DataSource = actions.OrderByDescending(a => a.Key);
使用相應的asp代碼如下:
<asp:Repeater ID="rptActions" runat="server">
<ItemTemplate>
<li>
<%#Eval("Value") %>
</li>
</ItemTemplate>
</asp:Repeater>
提前致謝。
更新*************
對不起,我有數據綁定,但當我嘗試訂購列表時,我收到編譯錯誤:
'System.Collections.Generic.IList<System.Collections.Generic.IDictionary<System.DateTime, string>>'
未知方法'OrderByDescending(?)'
'System.Collections.Generic.IList<System.Collections.Generic.IDictionary<System.DateTime, string>>'
字典基本上是KeyValuePair的列表。 您正在嘗試綁定到遍歷每個KeyValuePair的枚舉,而不是每個字典。 您需要將List of Dictionary(KeyValuePair列表列表)展平為單個List。 我使用SelectMany擴展方法。 就像是:
actions.SelectMany(dict => dict.ToList()).OrderByDescending(a => a.Key);
在LINQ SelectMany運算符的A Visual Look中有一個很好的視覺示例。 在該示例中,我們有一組客戶,其中包含訂單集合,每個訂單包含一個項目列表。 他用類似的東西迭代所有項目
var flatListOfItems = personList.SelectMany(p => p.Orders).SelectMany(o => o.Items);
然而,圖形確實使文章變得很棒。 你真的可以看到發生了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.