Andrew Simpson
Andrew Simpson

Reputation: 7314

Using Linq with VB.NET

I am so sorry for asking such a thick question. I need to sort the characters in a string using vb.net.

I normally use C#.

This is my code:

Dim val1() As Char = txtInput.Text.Trim.ToArray
val1 = val1.OrderBy(c >= c).ToArray()

Tells me that 'c' is not declared.

I have imported these:

Imports System.Collections.Generic
Imports System.Linq

Please tell me what I am doing wrong?

Thanks,

Upvotes: 0

Views: 133

Answers (3)

Sehnsucht
Sehnsucht

Reputation: 5049

Alternatively you don't really need Linq or lambda to do this. You can use ToCharArray and sort (in place) that array :

Dim chars = someString.ToCharArray
Array.Sort(chars)

Upvotes: 1

dbasnett
dbasnett

Reputation: 11773

To sort the characters of a string with the result being a string:

    Dim s As String = txtInput.Text.Trim.OrderBy(Function(c) c).ToArray

And if you want the result to be a character array:

    Dim val1() As Char = txtInput.Text.Trim.OrderBy(Function(c) c).ToArray

Upvotes: 1

Chris
Chris

Reputation: 27599

You're still using c# syntax for your lambda expression. See http://msdn.microsoft.com/en-us/library/bb531253.aspx for details of Lambda expressions in vb.

In your case its as simple as

val1 = val1.OrderBy(Function (c) c ).ToArray

Upvotes: 1

Related Questions