c# - How is an Enumerable converted to a Dictionary? -


i have following code msdn sample:

if (sheetdata.elements<row>().where(r => r.rowindex == rowindex).count() != 0) {     row = sheetdata.elements<row>().where(r => r.rowindex == rowindex).first(); ... 

which refactored follows:

dictionary<uint, row> rowdic = sheetdata.elements<row>().todictionary(r => r.rowindex.value); if (rowdic[rowindex].count() != 0) {     row = rowdic[rowindex]; ... 

now, sensed if enumerable.todictionary<> method actually has enumerate through data, redundant, msdn documentation not how conversion takes place.

the alternative i'm thinking of using is:

var foundrow = sheetdata.elements<row>().where(r => r.rowindex == rowindex); if (foundrow.count() != 0) {     row = foundrow.first(); ... 

however, know possibly previous experiences faster , why.

thanks.

the cleaner alternative is:

var row = sheetdata.elements<row>()                    .firstordefault(r => r.rowindex == rowindex); if (row != null) {     // use row } 

that iterate through sequence once, , stop finds match.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -