C#/.NET/WPF:从 LINQ 获取子集,而不是 IEnumerable

发布于 2024-09-17 10:00:23 字数 1541 浏览 3 评论 0原文

我有一些简单的代码,如下所示:

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 来说就不那么热了。

 

A) 有没有办法以这种方式将类似 LINQ 的表达式传递给 WPF 控件?

B) 如果做不到这一点,有没有办法做类似的事情:

// Put things back, like the .ToArray() syntax
d.SkipWhile( kvp => kvp.Value < min).ToSortedDictionary();

C) 如果做不到这一点,是否有办法有条件地从实际字典中删除条目?

d.RemoveWhere( kvp => kvp.Value < min );

 
更新: 过滤没有达到我的预期。事实证明,我对 .SkipWhile() 的理解是错误的,基于一个糟糕的文档来源。

// 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 ]
...

我没想到 .Value 的行少于三。

按照答案中的说明,使用 .Where() 代替,解决了问题。

I have some trivial code that looks like this:

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

Now what I'd like to do is dynamically show a subset of the dictionary without having to recompute it.

// min is assigned somewhere else
lb.ItemsSource = d.SkipWhile( kvp => kvp.Value < min );  // THIS DOESN'T WORK

.SkipWhile() returns an IEnumerable, which is great for foreach loops, but not so hot for ItemsSource.

 

A) Is there a way to pass a LINQ-like expression to a WPF control in this manner?

B) Failing that, is there a way to do something like:

// Put things back, like the .ToArray() syntax
d.SkipWhile( kvp => kvp.Value < min).ToSortedDictionary();

C) Failing that, is there a way to remove entries from the actual dictionary conditionally?

d.RemoveWhere( kvp => kvp.Value < min );

 

UPDATE:
The filtering wasn't working from what I was expecting. Turns out my understanding of .SkipWhile() was incorrect based on a bum documentation source.

// 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 );
}

Resulted in:

Total = 33350
 Skip = 33350
[ 0, 1042 ]
[ 00, 52 ]
[ 000, 55 ] 
[ 001, 1 ]
[ 002, 1 ]
[ 003, 1 ]
...

I wasn't expecting the lines with .Value being less than three.

Using .Where() instead, as explained in the answer, resolved the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2024-09-24 10:00:23

SortedDictionary 按键排序,而不是按值排序,因此 SkipWhile 方法不会跳过值小于最小值的所有项目,而是从最小的键,并且仅跳过项目,直到找到值大于或等于最小值的项目。

如果要过滤掉值小于最小值的所有项目,请使用 Where 方法:

lb.ItemsSource = s.Where(kvp => kvp.Value < min);

这将创建一个根据需要从 SortedDictionary 读取的表达式。

The SortedDictionary is sorted on the key, not on the value, so the SkipWhile method will not skip all items where the value is less than the minimum, it will start at the smallest key and only skip items until it finds an item where the value is larger or equal to the minimum.

If you want to filter out all items where the value is less than the minimum, use the Where method:

lb.ItemsSource = s.Where(kvp => kvp.Value < min);

This creates an expression that reads from the SortedDictionary as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文