2

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?

Henrik
23.3k6 gold badges45 silver badges93 bronze badges
asked Dec 8, 2010 at 14:42
2
  • Query results are immutable, and => is not a assigment operator. Commented Dec 8, 2010 at 14:45
  • Your LINQ code does not rewrite, rather it creates a new collection Commented Dec 8, 2010 at 14:46

4 Answers 4

3

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.

answered Dec 8, 2010 at 14:45
Sign up to request clarification or add additional context in comments.

Comments

1

The return type is an IEnumerable, you need to convert to an array:

sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
answered Dec 8, 2010 at 14:49

1 Comment

Thanks :) I see the ToArray<string>() is not required, just ToArray(). Why is that?
1

use:

sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
answered Dec 8, 2010 at 14:45

Comments

0
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();
answered Dec 8, 2010 at 14:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.