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?

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.

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();

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();

Comments

0
sOutputFields = sOutputFields.Select(el => clsSQLInterface.escapeIncoming(el)).ToArray();

Comments

Your Answer

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.