[英]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.