简体   繁体   English

为什么在IEnumerable上进行FirstOrDefault搜索 <Row> 基于RowIndex的在调试窗口中不起作用?

[英]Why the FirstOrDefault search on IEnumerable<Row> based on RowIndex doesn't work in debug windows?

The below line of code, which does a FirstOrDefault search on IEnumerable based on RowIndex doesn't work in debug windows (in Watch,Quick watch and Immediate window). 下面的代码行(基于RowIndex在IEnumerable上执行FirstOrDefault搜索)在调试窗口(“监视”,“快速监视”和“即时”窗口)中不起作用。 It throws a System.NullReferenceException in these windows. 它在这些窗口中引发System.NullReferenceException。 I'm using Visual studio 2015 update 3. 我正在使用Visual Studio 2015更新3。

sheetdata.Descendants<Row>().FirstOrDefault(p => p.RowIndex.Value == 2U)

But when I convert it to list and do the same search it works in these debug windows. 但是,当我将其转换为list并执行相同的搜索时,它将在这些调试窗口中工作。 Why is this discrepancy? 为什么会有这种差异?

sheetdata.Descendants<Row>().ToList().FirstOrDefault(p => p.RowIndex.Value == 2U)

This discrepancy is not there when I run the code. 当我运行代码时,这种差异并不存在。 I can see this discrepancy only when I try to debug the code in these debug windows. 我只有在尝试在这些调试窗口中调试代码时才能看到这种差异。

在此处输入图片说明

I think the QueryProvider of OpenXml has a problem with handling of Convert.ToUInt32(2) , cause he needs to translate that into its own language (like a QueryProvider for SQL). 我认为OpenXml的QueryProvider在处理Convert.ToUInt32(2)遇到问题,因为他需要将其翻译成自己的语言(例如SQL的QueryProvider)。

You should try to avoid converts in predicates and do them upfront, cause not all QueryProviders support all functionalities and might throw exceptions (mostly NotSupportedException ). 您应该尝试避免在谓词中进行转换,并且提前进行转换,因为并非所有QueryProvider都支持所有功能,并且可能引发异常(主要是NotSupportedException )。

uint value = Convert.ToUInt32(2);
sheetdata.Descendants<Row>().FirstOrDefault(p => p.RowIndex.Value == value)

The call .ToList() will perform this in memory, cause it will read all values from your sheet and then finds the first matching value. 调用.ToList()将在内存中执行此操作,因为它将读取工作表中的所有值,然后找到第一个匹配的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM