I have one Dictionary and added some elements on it.for example,
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Content","Level0");
d.Add("gdlh","Level1");
d.Add("shows","Level2");
d.Add("ytye","Level0");
In C#, Dictionary keeps elements in natural order.But i now want to iterate those values from last to first(ie, Reverse order).i mean,
first i want to read ytye then shows,gdlh and finally Content.
Please guide me to get out of this issue...
Use LINQ Reverse, but note that does not reverse in place :
var reversed = d.Reverse();
But note that this is not a SortedDictionary , so the order is not necessarily guaranteed in the first place. Perhaps you want OrderByDescending
instead?
Maybe OrderByDescending
on the key. Like this:
d.OrderByDescending (x =>x.Key)
Foreach
like this:
foreach (var element in d.OrderByDescending (x =>x.Key))
{
}
它可用于Linq: d.Reverse()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.