[英]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.