Linq where子句问题

发布于 2024-09-13 07:12:46 字数 454 浏览 5 评论 0原文

我想我不久前在某个地方看到过一个 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 技术交流群。

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

发布评论

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

评论(2

从此见与不见 2024-09-20 07:12:46

该函数从未被调用的最可能原因是,如果您“where-ing”的序列没有元素,或者您根本没有调用枚举器。 LINQ 是惰性求值的,这意味着在您开始 foreachToList() 之前,您提供的任何表达式都不会被实际调用。

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 or ToList().

◇流星雨 2024-09-20 07:12:46
cells.Keys.Where(key => isWithinArea(key, area))
cells.Keys.Where(key => isWithinArea(key, area))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文