Linq where子句问题
我想我不久前在某个地方看到过一个 where
子句的示例,它调用了一个给出 bool
结果的函数,但我找不到它了,所以我'我会概述我的问题。
我有一个集合
Dictionary< string, KeyValuePair < int, int >>
,我想在其中查询 string
键。表面上这很简单,但不幸的是,字符串(我无法控制)是网格单元的编码坐标。
查询是检索属于网格特定部分的条目(作为Dictionary
类型)。放置函数调用 cells.Where(c => isWithinArea(c.Key, area))
可以编译,但不起作用。该函数永远不会被调用。
我欢迎任何建议,让这项工作尽可能顺利进行。
I thought I'd seen somewhere a while back an example of where
clause which called a function that gave a bool
result ,and I can't find it again so I'll outline my problem.
I have a collection
Dictionary< string, KeyValuePair < int, int >>
in which I want to have a query for the string
key. On the surface that is simple but unfortunately the string (over which I have no control) is an encoded co-ordinate of a grid cell.
The query is to retrieve entries (as the type of Dictionary
) which fall into a certain part of the grid. Placing a function call cells.Where(c => isWithinArea(c.Key, area))
compiles but does not work. The function never gets called.
I would welcome any suggestions to make this work with a minimum of fuss.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该函数从未被调用的最可能原因是,如果您“where-ing”的序列没有元素,或者您根本没有调用枚举器。 LINQ 是惰性求值的,这意味着在您开始
foreach
或ToList()
之前,您提供的任何表达式都不会被实际调用。The most likely reasons the function is never called would be if the sequence you're "where-ing" has no elements, or you are never invoking the enumerator at all. LINQ is lazy-evaluated, which means none of the expressions you've provided are actually invoked until you start to
foreach
orToList()
.