在不获取 IndexOutOfRangeException 的情况下检查 IEnumerable<object> 是否有任何值

Check if IEnumerable<object> has any values inside without getting IndexOutOfRangeException

我有一个IEnumerable<object>。我想检查 IEnumerable<object> 里面是否有任何值。

我的方法是这样的:

private static JObject TransformExcelDatasource(Stream originalDatasource)
{
            // Read Excel Data
            var excelReader = new ExcelReader(originalDatasource);
            var sheetReader = excelReader[0];

            //Check if row has any values
            var row = sheetReader.Row(100);
            var rowHasValues = row.Any();
            if (rowHasValues)
            {
                //do stuff
            }
}

我尝试获取只有 3 行的 excel sheet 的第 100 行。我知道,这只是一个 POC。

var rowHasValues = row.Any(); 抛出 System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

这是变量 IEnumerable<object> row 的样子:

我尝试了这个问题的答案 (, second),但没有任何效果。

如何在不出现 IndexOutOfRangeException 的情况下检查 IEnumerable<object> row 是否有任何值?

更新

这是抛出的 IndexOutOfRangeException

row.Current 也不是一个选项,因为它 IEnumerable<object> 不包含它。

完整堆栈跟踪:

   at LightWeightExcelReader.SheetReader.get_Item(String cellAddress)
   at LightWeightExcelReader.SheetReader.<get_Item>b__8_1(String x)
   at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at NR.DataAdapter.Core.Services.TransformDatasourceService.TransformExcelDatasource(Stream originalDatasource) in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\Services\TransformDatasourceService.cs:line 35
   at NR.DataAdapter.Core.Services.TransformDatasourceService.TransformDatasource(IDatasource interpretedDatasource) in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\Services\TransformDatasourceService.cs:line 19
   at NR.DataAdapter.Core.DataAdapter.AdaptDatasources() in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\DataAdapter.cs:line 24
   at NR.DataAdapter.UI.Controllers.HomeController.Dashboard() in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.UI\Controllers\HomeController.cs:line 24
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

我相信您可以在尝试访问行索引之前检查 MaxRow 属性。

private static JObject TransformExcelDatasource(Stream originalDatasource)
{
    // Read Excel Data
    var excelReader = new ExcelReader(originalDatasource);
    var sheetReader = excelReader[0];

    // Check if the row is not out of limits.
    if (sheetReader.MaxRow < 100) return null;

    // Check if row has any values
    var row = sheetReader.Row(100);
    var rowHasValues = row.Any();
    if (rowHasValues)
    {
        //do stuff
    }
}