LINQ 查询无一例外地跳过。 为什么?
我有一个获取嵌套数组作为参数的方法:
Number[][] data
其中 Number 是继承自 INotifyPropertyChange 的非常简单的类。
然后我有这样的声明:
double[] max = (double[])data.Select(crArray => crArray.Select( cr => cr.ValueNorm ).Max());
当我尝试在调试器中观看它时,它只是跳过它并且整个方法,但没有例外弹出。
答案是什么? 以及更好的方法是什么。
当然,我可以使用循环 - 但它看起来又脏又长。 所以 LINQ 是更好的
选择 我使用的是VS2008 SP1
I have a method that gets a nested array as parameter:
Number[][] data
where Number is my very simple class that inherits from INotifyPropertyChange.
And then i have a statement like this:
double[] max = (double[])data.Select(crArray => crArray.Select( cr => cr.ValueNorm ).Max());
When I'm trying to watch it in the debugger it just skips it and the whole method though no exception pops out.
What is the answer? And what is the better way to do it.
I can use loops, of course - but it looks so dirty and long. So LINQ is preferrable
P.S. I'm using VS2008 SP1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定 Select 方法返回一个数组吗? 转换 + 泛型 = 代码味道
试试这个:
转换方法 - (ToArray) 分配一个新数组并填充它。 受方法的限制。
向下转换 - 允许您更改对象实例的引用类型。
仅允许实际实例类型或实例类型继承的某种类型。 其他任何情况都会引发运行时异常。
显式转换运算符——使用向下转换的语法,达到转换的效果。 这确实是一种转换方法。 对于试图理解选角的人们来说,这是一个无尽的困惑源。
考虑一下这段代码:
为什么在 vs 中没有得到运行时异常? 我不知道。
Are you sure that the Select method returns an array? Casting + generics = code smell
Try this:
Conversion Method - (ToArray) allocates a new array and populates it. Bound by the restrictions of methods.
Down Casting - Allows you to change the type of the reference to an object instance.
Only the actual instance type, or some type that the instance type inherits from are allowed. Anything else gives a runtime exception.
Explicit Conversion Operator - uses the syntax of down casting, to achieve the effect of conversion. It's really a Conversion Method. This is an endless source of confusion for people trying to understand casting.
Consider this code:
Why didn't you get a runtime exception in vs? I don't know.
通常这种类型的行为意味着您的源代码文件不是最新的。 尝试从输出目录中删除所有 .bin 和 .pdb 文件并重建。
Usually this type of behaviour means that your source code files are not up to date. Try deleting all .bin and .pdb files from the output directory and rebuild.
您是否尝试过迭代 max ?
我认为这没有抛出,因为您没有迭代请求的结果(Linq 仅在第一次调用 GetEnumerator 后才启动您的请求。ToArray
方法迭代您的请求的可枚举,并复制数组中的所有内容。我认为这就是区别。
Have you try to iterate through max ?
I think this did not throw, because you have not iterated through the result of your request (Linq launch your request only after the first call to GetEnumerator.
The ToArray method iterates throught the enumerable of your request, and copy every thing in an array. I think it's the difference.