简体   繁体   中英

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). It throws a System.NullReferenceException in these windows. I'm using Visual studio 2015 update 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. 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).

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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