繁体   English   中英

如何将IDictionary对象的IList绑定到数据源的集合?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM