質問

I have the following declarations:

IList<IDictionary<DateTime, string>> actions = new List<IDictionary<DateTime, string>>();
IDictionary<DateTime, string> d;

Then several times I add to actions, for example:

d = new Dictionary<DateTime, string>();
d.Add(DateTime.Now, "Must select at least one Policy to assign.");
actions.Add(d);

And at the end, I want to be able to assign this List of Dictionary objects to a data source. Something like:

rptActions.DataSource = actions.OrderByDescending(a => a.Key);

with the corresponding asp code as follows:

<asp:Repeater ID="rptActions" runat="server">
   <ItemTemplate>
      <li>
         <%#Eval("Value") %>
      </li>
   </ItemTemplate>
</asp:Repeater>

thanks in advance.

Updated*************

Sorry, I do have the databind but when I try to order the list I get a compile error:

Unknown method 'OrderByDescending(?)' of 'System.Collections.Generic.IList<System.Collections.Generic.IDictionary<System.DateTime, string>>'

役に立ちましたか?

解決

A dictionary is basically a list of KeyValuePair. You are trying to bind to an enumerable that iterates over each KeyValuePair, not each dictionary. You'll need to flatten the List of Dictionary (List of List of KeyValuePair) into a single List. I'd use the SelectMany extension method. Something like:

actions.SelectMany(dict => dict.ToList()).OrderByDescending(a => a.Key);

There is a great visual example at A Visual Look At the LINQ SelectMany operator. In the example, we have a collection of customers which have a collection of orders, and each order contains a list of items. He iterates over all the items with something like

 var flatListOfItems = personList.SelectMany(p => p.Orders).SelectMany(o => o.Items);

The graphics really make the article great, though. You can really see what's happening.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top