Search code examples
c#linqepplus

Finding a value with Epplus


I'm trying to use the code to find the address of a certain value in a Worksheet using Epplus and Linq. The value is in column D (4), but could be in any cell However, the following error is displayed

Linq Code

var query3 = (from cell in sheet.Cells["d:d"]
    where cell.Value.ToString().Equals("CRÉDITOS")
    select cell);

Error in Results View:

   at ExcelTests.Form1.<>c.<button1_Click>b__1_0(ExcelRangeBase cell)
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

enter image description here


Solution

  • As @krillgar suggested, you should rewrite the linq statement to include the possibility of Value returning null.

    var query3 = 
        from cell in sheet.Cells["d:d"]
        where cell.Value?.ToString() == "CRÉDITOS"
        select cell;