[英]How to get linq `ForEach` statement to return data on the method call being made for each list object?
I have a linq ForEach
statement that calls a method for each Report
object in the list. 我有一个linq
ForEach
语句,它为列表中的每个Report
对象调用一个方法。 This method returns an array of data tables for each call and I want to somehow get that returned data. 此方法为每个调用返回一个数据表数组,我想以某种方式获取返回的数据。 How can I do this using linq ForEach rather than the old school
foreach (var x in x's) { ... }
? 我怎样才能使用linq ForEach而不是旧学校的
foreach (var x in x's) { ... }
? Here is my code: 这是我的代码:
Reports.ForEach(r => r.LoadTableData(Event, Human, Animal, exData));
How can I get back each DataTable[]
that LoadTableData
is returning? 我怎样才能找回
LoadTableData
返回的每个DataTable[]
?
Use Select
instead: 改为使用
Select
:
var tables = Reports.Select(r => r.LoadTableData(Event, Human, Animal, exData));
Select
maps a collection onto another (in this case, a collection of Report
into a collection of DataTable[]
). Select
将集合映射到另一个集合(在本例中,将Report
的集合映射到DataTable[]
的集合中)。 In fact, the Select
method is often called map
in other languages such as Scala and Ruby. 实际上,
Select
方法通常在其他语言中称为map
,例如Scala和Ruby。
ForEach
executes an arbitrary action for each element in the source collection. ForEach
为源集合中的每个元素执行任意操作。
And btw, ForEach
is not part of the LINQ extensions. 顺便说一下,
ForEach
不是LINQ扩展的一部分。 It's just another method on List<T>
. 这只是
List<T>
上的另一种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.