LINQ to Object - 相当于for循环 [英] LINQ to Object - equivalent of for loop
本文介绍了LINQ to Object - 相当于for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
LINQ中的等价物如何:
int imp = 0;
for(int i = 0; i< 10; i ++)
{{br /> imp = imp + i;
我有以下内容:
var varq = Enumerable.Range(0,10)
<但是我如何在上面的for循环中增加imp。
TIA!
What is the equivalent of this in LINQ:
int imp = 0;
for (int i = 0; i < 10; i++)
{
imp = imp + i;
}
I have the following:
var linq = Enumerable.Range(0, 10)
But how do I increment imp as I did in the for loop above.
TIA!
推荐答案
您需要一个聚合运算符 - 在这种情况下 Sum 将执行:
int imp = Enumerable.Range(0 ,10).Sum();
在更复杂的场景中,您可以使用汇总方法。
乔
You need an Aggregation operator - in this case Sum will do:
int imp = Enumerable.Range (0, 10).Sum();
In more complex scenarios, you can use the Aggregate method.
Joe
这篇关于LINQ to Object - 相当于for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文