Search code examples
c#epplus

Loop thru ExcelPackage result for linq


Below code will query the specified condition within the sheet using EPPlus. Note that the result will have 2 columns.

var objs = from cell in worksheet.Cells["a:a"]
where cell.Value.ToString().Contains(textKey)
select worksheet.Cells["a:b"];

How will I loop through column [2] of the result like something below?

foreach (var item in objs[columnn 2])
{
    textKeys.Add(item.Value.ToString());
 }

Solution

  • objs contains the list of records with both the columns interchangeably like A1, B1, A2, B2... Please check if below works for you:

       var result = objs.FirstOrDefault();
    
       if (result != null && result.Any())
       {
          var filterResult = result.Where(x => x.Address.Contains("B")); 
       }