使用 SortedDictionary TakeWhile 返回空
TakeWhile
扩展方法的工具提示中有以下注释:“元素的索引用于谓词函数的逻辑”。请注意,此工具提示在普通工具提示中不可见,而仅在变量 sortedDictionary
的列表成员的智能感知列表的工具提示中可见。
这正是与 SortedDictionary
结合使用所需要的。
var first = sortedDictionary.First(kv => kv.Key.ToString().StartsWith(searchkey));
var last= sortedDictionary.Last(kv => kv.Key.ToString().StartsWith(searchkey));
var range = sortedDictionary.TakeWhile(kv => kv.Key.ToString().StartsWith(searchkey));
第一个和最后一个项目已找到并且正确,但是我的收藏范围是空的。
这里有什么问题吗?我期望一个范围包含第一个和最后一个之间的所有项目,包括它们。
我仍然可以使用第一个和最后一个找到范围,但 TakeWhile
使用索引,而 First
和 Last
显然不使用索引。
编辑:“使用索引”结果与排序无关,但您可以在查询中使用索引。例如,当我用 SortedList
替换 SortedDictionary
时,我可以这样做:
int ix1 = sortedList.IndexOfKey(first.Key);
int ix2 = sortedList.IndexOfKey(last.Key);
var range = sortedList.SkipWhile((x, i) => i < ix1).TakeWhile((x, i) => i <= ix2);
同样使用 SortedDictionary
我可以这样做:
var range = sortedList.SkipWhile(x => !x.Key.ToString().StartsWith(searchkey))
.TakeWhile(x => x.Key.ToString().StartsWith(searchkey));
我必须测试哪种方法更快,而且必须测试Where 查询。
The TakeWhile
extension method has the following comment in its tooltip: "the element's index is used in the logic of the predicate function". Note that this tooltip is not visible in the normal tooltip but only in the tooltip of the list of intellisense for the lists members of the variable sortedDictionary
.
That's exactly what is was looking for in combination with a SortedDictionary
.
var first = sortedDictionary.First(kv => kv.Key.ToString().StartsWith(searchkey));
var last= sortedDictionary.Last(kv => kv.Key.ToString().StartsWith(searchkey));
var range = sortedDictionary.TakeWhile(kv => kv.Key.ToString().StartsWith(searchkey));
The items first and last are found and correct, however my collection range is empty.
What is wrong here? I expected a range with all items between first and last including them.
I could still find the range using first and last but TakeWhile
uses the index while First
and Last
apparantly don't.
EDIT: "using the index" turns out to have nothing to do with sorting but you can use the index in the query. E.g. when I replace SortedDictionary
with SortedList
I can do:
int ix1 = sortedList.IndexOfKey(first.Key);
int ix2 = sortedList.IndexOfKey(last.Key);
var range = sortedList.SkipWhile((x, i) => i < ix1).TakeWhile((x, i) => i <= ix2);
Also with SortedDictionary
I can do:
var range = sortedList.SkipWhile(x => !x.Key.ToString().StartsWith(searchkey))
.TakeWhile(x => x.Key.ToString().StartsWith(searchkey));
I will have to test which method is faster and also have to test the Where query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果序列中的第一个元素与谓词不匹配,则 TakeWhile 方法将退出:
First 方法将采用第一个与您的谓词匹配的元素:
与第一种方法相反,最后 方法将采用最后一个方法与您的谓词匹配的元素:
我猜测 TakeWhile 会提前退出,因为迭代开始时它们没有与条件匹配的元素。
如果您想要示例中第一个和最后一个元素之间的元素范围(包括第一个和最后一个项目),请尝试以下操作:
或者您可以像我一样不要过度思考,并使用更简单的方法,使用
Where
方法如 Jeff 的示例。If the first element in your sequence does not match your predicate, then the TakeWhile method will exit:
The First method will take the first element that matches your predicate:
Opposite of the First method, the Last method will take the last element that matches your predicate:
I'm guessing that the TakeWhile is exiting early because their are no elements matching the condition when the iteration begins.
If you want the range of elements between the first and last element from your example (including both the first and last item), then try this:
Or you could just not over-think this like me and use a simpler approach using the
Where
method as in Jeff's example.TakeWhile()
将从序列开头开始获取项目,只要它们都满足谓词即可。如果第一项未通过谓词,则结果为空。您正在讨论的描述来自 重载,其中包含索引 (你没有使用)。只需正常过滤项目,它应该会产生您期望的范围(由于排序)。
TakeWhile()
will take items starting from the beginning of the sequence as long as they all satisfy the predicate. If the first item fails the predicate, the result is empty. The description you are talking about is from the overload that includes an index (which you aren't using).Just filter items normally and it should yield the range you are expecting (due to the sorting).
您阅读的工具提示适用于其谓词具有 Int32 作为参数的重载 MSDN 链接
您介意发布一些代码来看看为什么它返回空范围吗?以下代码按预期工作(我复制/粘贴了您的代码)。
The tooltip you read is for an overload whose predicate has a Int32 as a parameter MSDN Link
Would you mind posting some code as to see why it's returning an empty range?. The following code works as expected (I copy/pasted your code).