我有一些琐碎的代码如下所示:
SortedDictionary<String, long> d = new SortedDictionary<String, long>();
// d is then populated with an expensive time-consuming process
ListBox lb = new ListBox();
lb.ItemsSource = d; // THIS WORKS GREAT
现在我要做的是动态显示字典的一个子集,而不必重新计算它。
// min is assigned somewhere else
lb.ItemsSource = d.SkipWhile( kvp => kvp.Value < min ); // THIS DOESN'T WORK
.SkipWhile()返回一个IEnumerable,这对于foreach循环很好,但是对于ItemsSource就不那么热门了。
B) 如果做不到这一点,有没有办法做到:
// Put things back, like the .ToArray() syntax
d.SkipWhile( kvp => kvp.Value < min).ToSortedDictionary();
C) 如果做不到这一点,有没有办法有条件地从实际词典中删除条目?
d.RemoveWhere( kvp => kvp.Value < min );
更新
:
// This is what I was seeing and seemed strange
Console.WriteLine("Total = " + dict.Count");
Console.WriteLine(" Skip = " + dict.SkipWhile( kvp => kvp.Value < 3 ).Count());
foreach ( var kvp in dict.SkipWhile( kvp => kvp.Value < 3 ) ) {
Console.WriteLine( kvp );
}
导致:
Total = 33350
Skip = 33350
[ 0, 1042 ]
[ 00, 52 ]
[ 000, 55 ]
[ 001, 1 ]
[ 002, 1 ]
[ 003, 1 ]
...
我没想到。值小于3的行。