I have this routine which alters all elements within an array...
for (int i = 0; i < sOutputFields.GetUpperBound(0); i ++)
{
sOutputFields[i] = clsSQLInterface.escapeIncoming(sOutputFields[i]);
}
sOutputFields is a one dimensional string array. escapeIncoming() is a function which returns a string.
I thought this could be re-written thus..
sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el));
..but this appears to do nothing (though does not throw an exception). So I tried..
sOutputFields =
(string[])sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el));
..but I get this exception at execution time..
"Unable to cast object of type 'WhereSelectArrayIterator`2[System.String,System.String]' to type 'System.String[]'."
how to fix?
-
Query results are immutable, and => is not a assigment operator.asawyer– asawyer2010年12月08日 14:45:21 +00:00Commented Dec 8, 2010 at 14:45
-
Your LINQ code does not rewrite, rather it creates a new collectionMax– Max2010年12月08日 14:46:24 +00:00Commented Dec 8, 2010 at 14:46
4 Answers 4
A Select doesn't return an object that can be explicitly cast to an array. You'd need to do sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray<string>() in your assignment.
Comments
The return type is an IEnumerable, you need to convert to an array:
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
1 Comment
use:
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
Comments
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();