C# 将条件 lambda 语句与列表上的 foreach 操作一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using conditional lambda statements with a foreach Action on a list
提问by Alex
Why Cant I do something like this?
为什么我不能做这样的事情?
If I have a List<String> myList
populated with items, I want to be able to act on each member in a conditional way like so:
如果我有一个List<String> myList
填充了项目,我希望能够以有条件的方式对每个成员采取行动,如下所示:
myList.ForEach(a => { if (a.Trim().Length == 0) a = "0.0"; })
But this will not compile. Im guessing its something to do with missing a return value?
但这不会编译。我猜它与缺少返回值有关吗?
Im trying to prepare a list of strings for conversion to doubles, and I want the empty items to show '0.0' so I can just convert the whole list in one go.
我试图准备一个字符串列表以转换为双打,我希望空项显示“0.0”,这样我就可以一次性转换整个列表。
采纳答案by Lasse V. Karlsen
ForEach is not mutable, it doesn't change the data structure in any way.
ForEach 不是可变的,它不会以任何方式改变数据结构。
You can either:
您可以:
- Handle the loop yourself, with an index
- Produce a new list, using .Select and .ToList (provided you're using .NET 3.5)
- 自己处理循环,使用索引
- 使用 .Select 和 .ToList 生成一个新列表(前提是您使用的是 .NET 3.5)
Example of #1:
#1 的示例:
for (Int32 index = 0; index < myList.Count; index++)
if (myList[index].Trim().Length == 0)
myList[index] = "0.0";
With .NET 3.5 and Linq:
使用 .NET 3.5 和 Linq:
myList = (from a in myList
select (a.Trim().Length == 0) ? "0.0" : a).ToList();
With .NET 3.5, not using the Linq-syntax, but using the Linq-code:
在 .NET 3.5 中,不使用 Linq 语法,而是使用 Linq 代码:
myList = myList.Select(a => (a.Trim().Length == 0) ? "0.0" : a).ToList();
Edit: If you want to produce a new list of doubles, you can also do that in one go using Linq:
编辑:如果你想生成一个新的双打列表,你也可以使用 Linq 一次性完成:
List<Double> myDoubleList =
(from a in myList
select (a.Trim().Length == 0 ? "0" : a) into d
select Double.Parse(d)).ToList();
Note that using "0.0" instead of just "0" relies on the decimal point being the full stop character. On my system it isn't, so I replaced it with just "0", but a more appropriate way would be to change the call to Double.Parse to take an explicit numeric formatting, like this:
请注意,使用“0.0”而不是“0”依赖于小数点是句号。在我的系统上它不是,所以我只用“0”替换它,但更合适的方法是将调用更改为 Double.Parse 以采用显式数字格式,如下所示:
List<Double> myDoubleList =
(from a in myList
select (a.Trim().Length == 0 ? "0.0" : a) into d
select Double.Parse(d, CultureInfo.InvariantCulture)).ToList();
回答by Daniel Earwicker
What you possibly want is:
你可能想要的是:
public static void MutateEach(this IList<T> list, Func<T, T> mutator)
{
int count = list.Count;
for (int n = 0; n < count; n++)
list[n] = mutator(list[n]);
}
Which would allow:
这将允许:
myList.MutateEach(a => (a.Trim().Length == 0) ? "0.0" : a);
Just put the MutateEach
method in a public static class of your own and make sure you have a using directive that will find it.
只需将该MutateEach
方法放在您自己的公共静态类中,并确保您有一个可以找到它的 using 指令。
Whether it's worth defining something like this depends on how often you'd use it. Note that it has to be an extension on IList
instead of IEnumerable
, so we can perform the updates, which makes it less widely applicable.
是否值得定义这样的东西取决于你使用它的频率。请注意,它必须是 onIList
而不是的扩展IEnumerable
,因此我们可以执行更新,这使得它不太适用。
回答by Brett Caswell
@daniel-earwicker's MutateEach
Extension solution is a good solution.
@daniel-earwicker 的MutateEach
扩展解决方案是一个很好的解决方案。
Though, for returning a new List
, you have the option to use List.ConvertAll<TTo>(Converter<TFrom,TTo>(Func<TFrom, TTo>))
, which has been around since .NET 2.0.
但是,为了返回一个 new List
,您可以选择使用List.ConvertAll<TTo>(Converter<TFrom,TTo>(Func<TFrom, TTo>))
,它从 .NET 2.0 开始就已经存在了。
It is not Linq, or Lambda.
它不是Linq或Lambda。
List<String> nodeStringList = new List<String>(new String[] { "", "0.5 ", " 1.1"});
nodeStringList = nodeStringList.ConvertAll<String>((c) => (c.Value.Trim().Length != 0) ? c.Value.Trim() : "0.0");