简体   繁体   English

检查 IEnumerable<object> 里面有任何值而没有得到 IndexOutOfRangeException<div id="text_translate"><p> 我有一个IEnumerable&lt;object&gt; 。 我想检查IEnumerable&lt;object&gt;里面是否有任何值。</p><p> 我的方法如下所示:</p><pre> 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 } }</pre><p> 我尝试获取只有 3 行的 excel 表的第 100 行。 我知道,这只是一个 POC。</p><p> 行var rowHasValues = row.Any(); throws System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'</p><p> 这是变量IEnumerable&lt;object&gt; row的样子: <a href="https://i.stack.imgur.com/quYVJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/quYVJ.png" alt="在此处输入图像描述"></a></p><p> 我尝试了这个问题的答案( <a href="https://stackoverflow.com/questions/52575358/ienumerable-indexoutofrangeexception" rel="nofollow noreferrer">第一个</a>, <a href="https://stackoverflow.com/questions/27277534/index-out-of-bounds-in-linq-enumerable" rel="nofollow noreferrer">第二个</a>),但对我没有任何帮助。</p><p> <strong>如何检查IEnumerable&lt;object&gt; row是否有任何值而不获得 IndexOutOfRangeException?</strong></p><p> <strong>更新</strong></p><p>这是抛出的 IndexOutOfRangeException <a href="https://i.stack.imgur.com/SJN18.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SJN18.png" alt="![在此处输入图像描述"></a></p><p> row.Current也不是一个选项,因为它IEnumerable&lt;object&gt;不包含它。 <a href="https://i.stack.imgur.com/GshWk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GshWk.png" alt="在此处输入图像描述"></a></p><p> 完整的堆栈跟踪:</p><pre> at LightWeightExcelReader.SheetReader.get_Item(String cellAddress) at LightWeightExcelReader.SheetReader.&lt;get_Item&gt;b__8_1(String x) at System.Linq.Utilities.&lt;&gt;c__DisplayClass2_0`3.&lt;CombineSelectors&gt;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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()</pre></div></object>

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

I have a IEnumerable<object> .我有一个IEnumerable<object> I would like to check if the IEnumerable<object> has any values inside.我想检查IEnumerable<object>里面是否有任何值。

My method looks like this:我的方法如下所示:

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
            }
}

I try to get the 100th row of a excel sheet which only has 3 rows.我尝试获取只有 3 行的 excel 表的第 100 行。 I know that, this is just a POC.我知道,这只是一个 POC。

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

This is how the variable IEnumerable<object> row looks like:这是变量IEnumerable<object> row的样子: 在此处输入图像描述

I tried answers from this questions ( first , second ), but nothing worked for me.我尝试了这个问题的答案( 第一个第二个),但对我没有任何帮助。

How can I Check if the IEnumerable<object> row has any values without getting a IndexOutOfRangeException?如何检查IEnumerable<object> row是否有任何值而不获得 IndexOutOfRangeException?

UPDATE更新

This is the IndexOutOfRangeException thrown这是抛出的 IndexOutOfRangeException ![在此处输入图像描述

row.Current is also not a option since it IEnumerable<object> doesn't contain it. row.Current也不是一个选项,因为它IEnumerable<object>不包含它。 在此处输入图像描述

Full Stacktrace:完整的堆栈跟踪:

   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()

I believe you can check the MaxRow property, before trying to access the Row index.我相信您可以在尝试访问 Row 索引之前检查 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM