如何改变List< int>与LINQ ForEach [英] How alter List<int> with LINQ ForEach

查看:152
本文介绍了如何改变List< int>与LINQ ForEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List< int> myInts ,并希望乘以10.我想要使用linq(不是foreach循环)。我尝试了这个,但没有发生:

I have a List<int> myInts and want to multiply all with 10. I want to use linq (not foreach loop).I tryed this but nothing happend:

List<int> myInts = new List<int>() { 1, 2, 3 };
myInts .ForEach(act => act=act*10);

关于 .ForEach(.. 。)部分?是的,我想要使用ForEach,如果它是某种可能的话。

Of what do I have to take care in the .ForEach(...) part? And yes, I want to use ForEach if it is somehow possible.

可能它简单,但我不能看到它,我apoligize。谢谢大家!

Probably its simple, but I cant see it, I apoligize. Thank you all!

推荐答案

这会创建一个List的新实例。

This creates a new instance of List.

myInts = myInts.Select(p=>p*10).ToList();

这篇关于如何改变List&lt; int&gt;与LINQ ForEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆