[英]Rewrite this array manipulation using a lambda function?
我有这个例程可以更改数组中的所有元素...
for (int i = 0; i < sOutputFields.GetUpperBound(0); i ++)
{
sOutputFields[i] = clsSQLInterface.escapeIncoming(sOutputFields[i]);
}
sOutputFields
是一维string
数组。 escapeIncoming()
是一个返回string
的函数。
我认为可以这样重写。
sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el));
..但是这似乎什么也没做(尽管不会抛出异常)。 所以我尝试了..
sOutputFields =
(string[])sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el));
..但是我在执行时得到了这个异常。
“无法将类型为'WhereSelectArrayIterator`2 [System.String,System.String]'的对象转换为类型为'System.String []'的对象。”
怎么修?
Select不会返回可以显式转换为数组的对象。 您需要在分配中执行sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray<string>()
。
采用:
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
返回类型是IEnumerable,您需要转换为数组:
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.