简体   繁体   English

获取特定列中所有值的 IEnumerable

[英]Getting an IEnumerable of all values in a certain column

I'm currently working with the Atata framework and using a Kendo grid.我目前正在使用 Atata 框架并使用 Kendo 网格。 What I'm trying to achieve is getting an IEnumerable or a collection containing all the values in a column.我想要实现的是获取一个 IEnumerable 或一个包含列中所有值的集合。 The kendo grid is defined as below:剑道网格定义如下:

public KendoGrid<TransactionGroupsRow, _> TransactionGroupsGrid { get; private set; }

public class TransactionGroupsRow : KendoGridRow<_>
    {
        [FindByColumnHeader("Group Name")]
        public Link<_> GroupName { get; private set; }
    }

I not only want a collection of all the links GroupName, I specifically want the text component of them ie what the link would read like on the screen.我不仅想要所有链接 GroupName 的集合,我还特别想要它们的文本组件,即链接在屏幕上的显示内容。

链接及其文本的示例

So in this example I would want an IEnumerable containing "002999" and "003999".所以在这个例子中,我想要一个包含“002999”和“003999”的IEnumerable。

Let me know if I need to provide additional detail.让我知道是否需要提供更多详细信息。 Thanks in advance!提前致谢!

There are 2 ways to do that.有两种方法可以做到这一点。

  1. Use SelectData method.使用SelectData方法。 It works quite fast for list of items with count <= 50.它适用于计数 <= 50 的项目列表。

     // Just get all items: IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows.SelectData(x => x.GroupName.Content.Value).Value; // Assert items: page.TransactionGroupsGrid.Rows.SelectData(x => x.GroupName.Content.Value).Should.Contain("002999", "003999");
  2. The faster way is using SelectContentsByExtraXPath method.更快的方法是使用SelectContentsByExtraXPath方法。 This one will work very fast even for a table with 1000 rows.即使对于有 1000 行的表,这个也可以非常快地工作。 But you need to specify a relative XPath to the table cell td element inside a row tr element.但是您需要为行tr元素内的表格单元格td元素指定相对 XPath 。 This can also be extracted to the page object class as a method.这个也可以提取到页面object class作为方法。

     // Just get all items: IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows.SelectContentsByExtraXPath("td[1]", "Group Names").Value; // Assert items: page.TransactionGroupsGrid.Rows.SelectContentsByExtraXPath("td[1]", "Group Names").Should.Contain("002999", "003999");

You can also check Performance Practices for ControlList Atata sample project.您还可以查看ControlList Atata 示例项目的性能实践

检查 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

暂无
暂无

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

相关问题 将列值返回为IEnumerable - return column values as IEnumerable 从列表中获取值<IEnumerable> - Getting Values out of List<IEnumerable> 将所有值乘以IEnumerable <int> - Multiplying all values in IEnumerable<int> 从无限循环中选择某个列值 - selecting a certain column value from looping in ienumerable 调试时从datarow获取所有列名/列值 - Getting all column names / column values from datarow when debugging LINQ用于获取IEnumerable的所有条目 <string> 以相同的字符开头 - LINQ for getting all entries of a IEnumerable<string> which start with the same characters 从IEnumerable <Dictionary <string,object >>获取键和值 - Getting Keys and Values from IEnumerable<Dictionary<string, object>> 将IEnumerable的一列中的所有不同项分配给SelectList - Assign all distinct items from one column of IEnumerable to SelectList 检查 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 在特定目录中获取所有名称空间 - Getting all namespaces in certain directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM