LINQ 查询无一例外地跳过。 为什么?

发布于 2024-07-14 05:44:01 字数 420 浏览 4 评论 0原文

我有一个获取嵌套数组作为参数的方法:

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 技术交流群。

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

发布评论

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

评论(3

洛阳烟雨空心柳 2024-07-21 05:44:01

您确定 Select 方法返回一个数组吗? 转换 + 泛型 = 代码味道

试试这个:

double[] max = data
  .Select(crArray => crArray.Select( cr => cr.ValueNorm ).Max())
  .ToArray();

转换方法 - (ToArray) 分配一个新数组并填充它。 受方法的限制。

向下转换 - 允许您更改对象实例的引用类型。
仅允许实际实例类型或实例类型继承的某种类型。 其他任何情况都会引发运行时异常。

显式转换运算符——使用向下转换的语法,达到转换的效果。 这确实是一种转换方法。 对于试图理解选角的人们来说,这是一个无尽的困惑源。

考虑一下这段代码:

// reference type is IEnumerable<string>, instance type is string[]
IEnumerable<string> myArray =
  new string[3] { "abc", "def", "ghi" };
// reference type is IEnumerable<string>, instance type is ???
IEnumerable<string> myQuery = myArray
  .Select(s => s.Reverse().ToString());

//Cast - works
string[] result = (string[]) myArray;
//Cast - runtime exception: System.InvalidCastException
result = (string[])myQuery;
//Conversion - works
result = myQuery.ToArray();

为什么在 vs 中没有得到运行时异常? 我不知道。

Are you sure that the Select method returns an array? Casting + generics = code smell

Try this:

double[] max = data
  .Select(crArray => crArray.Select( cr => cr.ValueNorm ).Max())
  .ToArray();

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:

// reference type is IEnumerable<string>, instance type is string[]
IEnumerable<string> myArray =
  new string[3] { "abc", "def", "ghi" };
// reference type is IEnumerable<string>, instance type is ???
IEnumerable<string> myQuery = myArray
  .Select(s => s.Reverse().ToString());

//Cast - works
string[] result = (string[]) myArray;
//Cast - runtime exception: System.InvalidCastException
result = (string[])myQuery;
//Conversion - works
result = myQuery.ToArray();

Why didn't you get a runtime exception in vs? I don't know.

假扮的天使 2024-07-21 05:44:01

通常这种类型的行为意味着您的源代码文件不是最新的。 尝试从输出目录中删除所有 .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.

清晨说晚安 2024-07-21 05:44:01

您是否尝试过迭代 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.

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