How to convert c# linq code to vb.net code ?

Hakan
Hakan used Ask the Experts™
on
Hi,

I found useful  code that converts System.Array to String, but i couldn't succeed to implement to my vb.net application.

Any help would be great.

Thanks.

string[] foo = someObjectArray.OfType<object>().Select(o => o.ToString()).ToArray();

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
>>...code that converts System.Array to String

Wrong.  That code gives an array of strings, not a string.
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Hakan,

Please give this one a try:
someObjectArray.OfType(Of Object)().[Select](Function(o) o.ToString()).ToArray()

Open in new window


Regards,
Chinmay.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
If this truly is an array, then the OfType call is really extraneous here--having the Select alone should be sufficient.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
thanks for your contributions and advice. I'm really appreciate.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Agreed.  It could simply be:
Dim foo As String() = someObjectArray.Select(Function(s) s.ToString).ToArray

Open in new window


You could also use Array.ConvertAll():
Dim foo As String() = Array.ConvertAll(someObjectArray, Function(s) s.ToString)

Open in new window

AndyAinscowFreelance programmer / Consultant

Commented:
The big question is if one string is required as the result.  Note how the question was worded - it converts array to string

eg.
4 objects in an array each contain in order one of the following words "this" "is" "a" "string"
After running the code one has an array of four strings "this" "is" "a" "string"
The result is NOT one string "this is a string"

Author

Commented:
@Mike your code is give an error on visual studio editor. It says "Select" is not member of array.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Make sure:

Imports System.Linq

...is at the top of your file.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial