I have the following code from MSDN sample:
if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
{
row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
...
Which I refactored as follows:
Dictionary<uint, Row> rowDic = sheetData.Elements<Row>().ToDictionary(r => r.RowIndex.Value);
if (rowDic[rowIndex].Count() != 0)
{
row = rowDic[rowIndex];
...
Now, I sensed that if the Enumerable.ToDictionary<> method actually has to enumerate through all the data, then this would be as well redundant, but the MSDN documentation does not say anything about how this 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, I would like to know from possibly previous experiences which would be faster and why.
Thanks.