Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将For语句转换为LINQ_C#_Linq_Visual Studio_For Loop_Resharper - Fatal编程技术网

C# 如何将For语句转换为LINQ

C# 如何将For语句转换为LINQ,c#,linq,visual-studio,for-loop,resharper,C#,Linq,Visual Studio,For Loop,Resharper,我如何将for语句转换为LINQ 我想我有办法 public void Method_One(){ //...code... //...code... } 我必须多次调用这些代码,迭代次数也是我的参数 for(int i = 0; i < numbers.Length; ++i){ Method_One(); } for(int i=0;i

我如何将for语句转换为LINQ

我想我有办法

public void Method_One(){
     //...code...
     //...code...
}
我必须多次调用这些代码,迭代次数也是我的参数

for(int i = 0; i < numbers.Length; ++i){
    Method_One();
}
for(int i=0;i

我知道foreach语句有转换,但是foreach语句如何呢?

当您可以使用
foreach
时,您可以一直使用
for
,因此也可以使用一个简单的LINQ(asuming
numbers
是一个
列表
):否则,在使用
IEnumerable.ToList()之前将其转换为列表

这相当于

numbers.ForEach(x => Method_One());
而且:

foreach(int i in numbers) Method_one();

当您可以使用
foreach
时,您可以始终使用
for
,因此也可以使用一个简单的LINQ(asuming
numbers
是一个
列表
):否则在使用
IEnumerable.ToList()之前将其转换为列表

这相当于

numbers.ForEach(x => Method_One());
而且:

foreach(int i in numbers) Method_one();
您可以使用:

numbers.ForEach(num =>
{
  MethodOne();
});
如果您需要将当前数传递到函数中,则

MethodOne(num);
您可以使用:

numbers.ForEach(num =>
{
  MethodOne();
});
如果您需要将当前数传递到函数中,则

MethodOne(num);
同意@Ivan Stoev的观点,这仍然是一种使用linq的循环方式

一个简单的helper静态方法应该使它更具可读性,而不是Linq

public static void Repeater(int count, Action action)
{
    Debug.Assert(action!=null);
    for (int i = 0; i < count; i++)
        action();
}

//Use it like this
Repeater(10, Method_One);
公共静态无效中继器(整数计数、动作)
{
Assert(action!=null);
for(int i=0;i
同意@Ivan Stoev的观点,这仍然是一种使用linq的循环方式

一个简单的helper静态方法应该使它更具可读性,而不是Linq

public static void Repeater(int count, Action action)
{
    Debug.Assert(action!=null);
    for (int i = 0; i < count; i++)
        action();
}

//Use it like this
Repeater(10, Method_One);
公共静态无效中继器(整数计数、动作)
{
Assert(action!=null);
for(int i=0;i
如果您想强制Linq做某事,请具体化:

然而,Linq并不是为这种用途而设计的,因此查询看起来很难看。环状

  foreach (var item in numbers)
    Method_One(); // you may want Method_One(item);
是更好的代码。

如果您想强制Linq执行某些操作,请具体化:

然而,Linq并不是为这种用途而设计的,因此查询看起来很难看。环状

  foreach (var item in numbers)
    Method_One(); // you may want Method_One(item);

是更好的代码。

为什么要将其标记为ReSharper?该工具应该告诉您如何转换它。LINQ不是用于/foreach的
replacement。用于查询,而不是调用操作。为什么要将其标记为ReSharper?该工具应该告诉您如何转换它。LINQ不是用于/foreach的
replacement。用于查询,而不是调用操作。这里的
ForEach
是什么?嗯,
IEnumerable
没有
ForEach
方法。如果要使用ForEach,可以添加
ToList()
,但它仍然是一个overkill@KamilT绝对地特别是将其与去掉大括号的转换后的单行原件进行比较:
for(int i=0;i
既然
中继器是公共的,请验证
action
-如果
action==null怎么办?
(也可能是
count
)这里的
ForEach
是什么?嗯,
IEnumerable
没有
ForEach
方法。如果您想使用ForEach,可以添加
ToList()
,但这仍然是一个问题overkill@KamilT绝对地特别是将其与去掉大括号的转换后的单行原件进行比较:
for(int i=0;i
由于中继器是公共的,请验证
操作
-如果
操作==null怎么办?
(也可以是
计数